Question: #include #include common _ threads.h int done = 0 ; void * worker ( void * arg ) { printf ( this should
#include
#include "commonthreads.h
int done ;
void workervoid arg
printfthis should print first
;
done ;
return NULL;
int mainint argc, char argv
pthreadt p;
Pthreadcreate&p NULL, worker, NULL;
while done
;
printfthis should print last
;
return ;
What synchronized primitives can be added to above program to make the mutual exclusion effectively and effeciently Choose all options suitable.
Add join in the main thread to force it to wait for the completion of the worker thread.
Add a lock surrounding the global variable 'done' in both main and worker thread for controlled access.
Add condition varaiable to coordinate the action between main thread and worker thread. Specifically, add wait inside the while loop in the main to suspend the its execution until the worker thread completes the change on the global variable and notifies the main by invoking signal
Change the while done to if done to stop the spin waiting of main thread. This avoid wasting the CPU cycle.
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
