In this homework, you’re going to turn a single-threaded program into a multi-threaded program based on pthreads. HW3 is based on a “NoisySleeper” class, which represents a snoring person. Right now, when you invoke the bedTime() method, the sleeper falls asleep for a pre-determined amount of time (snoring the whole time!).

Right now, the main driver program creates three sleeping objects, and invokes them serially. The first sleeper sleeps, then wakes up, then the second sleeper sleeps, then wakes up, etc. In this homework you’re going to make the program rely on threads via the pthreads library, so that when the main program runs, the three objects’ sleeping time overlaps with each other. Further, you’re going to have each thread update a variable that keeps track of the total time spent sleeping, and the main thread/main method will print out that total value.

For this project you should use pthreads calls and the pthreads library to implement threading and mutexes.

Step 1: Clone the GitHub repository

Step 2: Modify the NoisySleeper class and/or the main function

Ensure that the bedTime() method overlaps across the three sleeper objects, and update the shared total time slept class variable within the bedTime() method under the protection of a pthreads-based synchronization primitive.

Hint: It is probably easier to create the three objects in the main thread, and also to create the three threads in main, and pass one object to each of the three threads.

Step 3: Push your changes back to the repository

Only files committed and pushed back to the repository can be graded.

Starter code

You can accept the HW2 invitation on GitHub which will provide you with the starter code. The link is:

If you have any questions, contact us!