Page 1 of 1
a) Implement a barrier for pthreads in C++ using pthread condition variables and mutexes in a file called barrier.cc. Co
Posted: Fri Jul 01, 2022 5:51 am
by answerhappygod

- A Implement A Barrier For Pthreads In C Using Pthread Condition Variables And Mutexes In A File Called Barrier Cc Co 1 (52.55 KiB) Viewed 53 times
a) Implement a barrier for pthreads in C++ using pthread condition variables and mutexes in a file called barrier.cc. Consider a process with N running threads. During their execution, all threads call repeatedly the barrier’s wait() method. For the 1ª, 2ªª, ..., (N-1)* call, the wait() function blocks (suspends) the calling thread. The Nth thread that calls this function will unblock all suspended threads and then will return. All threads that were previously blocked in wait() will now return from wait() and will resume their execution. They will continue until their next call to the barrier's wait() method. In effect, the slowest thread will determine the overall progress of the application. Note that the identity and type of thread are irrelevant in deciding to suspend it or "raise" the barrier in wait(): only the Nth call to wait() will resume all processes, while all other call to wait() will suspend the caller thread.
The Barrier class must have at least a public constructor, a destructor, and a method wait(), as seen in the following class skeleton: class Barrier { public: }; Barrier (int n); -Barrier(); void wait(); // ... // n is the number of threads (N) // destructor: cleanup, destroy mutex, condvar, etc. may need a condition variable, a mutex, and some other attributes A Barrier object must encapsulate all the necessary synchronization objects (mutex and condition variable, etc.) to do its job. Use the principle of encapsulation and keep the instance variables private. Do not use global variables.