Answered step by step
Verified Expert Solution
Question
1 Approved Answer
3. Barrier-35% a) Implement a barrier for pthreads in C++ using pthread condition variables and mutexes. Consider a process with N running threads. During their
3. Barrier-35% a) Implement a barrier for pthreads in C++ using pthread condition variables and mutexes. Consider a process with N running threads. During their execution, all threads call repeatedly the barrier's wait0 method. For the 1t, 2d, ., (N-1)th call, the wait function blocks (suspends) the calling thread. The N 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 wait0 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 wait0: only the N call to wait0 will resume all processes, while all other call to wait0 will suspend the caller thread Here is an illustration of a barrier from the Into to Parallel Computing guide from LLNL: task 0 task 1 task 2 task 4 work wait time (Analogy: imagine a real-world barrier on a highway. Cars that arrive at the barrier have to stop and wait for the barrier to be raised. For each Nth car that arrives, the barrier is raised and all N cars waiting can now proceed. Right after the last car passes, the barrier is lowered again.) A barrier is useful to control a group of threads so they all proceed in sync at specific points in the program. Here is how a barrier object may be used: /I the main thread: // barrier object declared as a global variable, to be accessible from all threads: Barrier barrier (N) / assume N was defined as an int somewhere / child threads run this thread function or similar void thread fun (void param) while (not_exit) [ /7 thread runs in a loop / do some work barrier.wait); // suspend calling threads until the Nth thread makes the call, after which all threads will resume and return from wait // do some more work
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started