Question: Thread 1 handles Player 1 s behavior; Thread 2 handles Player 2 s behavior. Both threads share a common structure. Loop forever Set min to
Thread handles Player s behavior; Thread handles Player s behavior. Both threads share a common structure.
Loop forever
Set min to and max to Lock mutex Using a while loop, wait on condition variable
In my code base this was implemented as a whilerdy condwait loop. The following loop is for thread
while rdy pthreadcondwait&cnd &mtx;
note: both players are waiting on the cnd mtx pairingUnlock mutex and set the rdy flag you were looping over to Loop forever
Calculate the childs guess using the proper method See belowStore that guess in the proper global variable.Lock the mutex associated with the threads guess See belowUsing a while loop, wait on the associated condition variable See belowUnlock the proper mutex See belowOnce this condition variable has been signaled, the value of the referees response should be stored in the cmp global. Using the proper element of the cmp global do the following:
If cmp set min to the current guessIf cmp set max to the current guessIf cmp is break.
I suggest using the following values for Thread :
use mtx as the thread's mutex for its guess
use cnd and rdy as your condition variable and associated state
use guess and cmp to store thread s guess and the referee's response
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
