Answered step by step
Verified Expert Solution
Link Copied!

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

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 barriers wait() method. For the 1st, 2nd, ..., (N-1)th 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 barriers 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. Here is an illustration of a barrier from the Intro to Parallel Computing guide from LLNL: (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: // 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) { // thread runs in a loop // do some work barrier.wait(); // suspend all calling threads until the Nth thread makes the call, // after which all threads will resume and return from wait // do some more work } } The Barrier class must have at least a public constructor, a destructor, and a method wait(): class Barrier { public: Barrier(int n); // n is the number of threads (N) ~Barrier(); // destructor: cleanup, destroy mutex, condvar, etc. void wait(); // may need a condition variable, a mutex, and some other attributes }; A Barrier object must encapsulate all necessary synchronization objects (mutex and condition variable, plus something else) to do its job. Use the principle of encapsulation and keep private the instance variables. b. Write a multithreaded application in C++ that demonstrates how your barrier works.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases A Beginners Guide

Authors: Andy Oppel

1st Edition

007160846X, 978-0071608466

More Books

Students also viewed these Databases questions

Question

5. Have you any experience with agile software development?

Answered: 1 week ago