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 1 handles Player 1s behavior; Thread 2 handles Player 2s behavior. Both threads share a common structure.
Loop forever
Set min to 0 and max to 100Lock mutex 3Using a while loop, wait on condition variable 3.
In my code base this was implemented as a while(!rdy) cond_wait() loop. The following loop is for thread 1
while (rdy[2]==0) pthread_cond_wait(&cnd[2], &mtx[2]);
(note: both players are waiting on the cnd[2], mtx[2] pairing)Unlock mutex 3 and set the rdy flag you were looping over to 0Loop forever
Calculate the childs guess using the proper method (See below)Store that guess in the proper global variable.Lock the mutex associated with the threads guess (See below)Using a while loop, wait on the associated condition variable (See below)Unlock the proper mutex (See below)Once 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 <0, set min to the current guessIf cmp >0, set max to the current guessIf cmp is 0, break.
I suggest using the following values for Thread 1:
use mtx[0] as the thread's mutex for its guess
use cnd[0] and rdy[0] as your condition variable and associated state
use guess[0] and cmp[0] to store thread 1'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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!