Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a C++ program that implements the following algorithm using the pthreads

ID: 646990 • Letter: C

Question

Create a C++ program that implements the following algorithm using the pthreads library:

Create two functions, one for each "singer", as follows:

     Singer 1 should repeat the following 12 times:

        Output the line "Row, row, row your boat,"

        Rest for 1 second

        Output the line "Merrily, merrily, merrily, merrily,"

        Rest for 1 second

     Singer 2 should repeat the following 12 times:

        Rest for 1 second

        Output "Gently down the stream,"

        Rest for 1 second

        Output "Life is but a dream."

From the main program, create two pthreads, one for each of the "singers".

Start both threads (in order).

You will need to prevent the main function from exiting until the threads have finished singing, so arrange to join each thread before the end of main.

Of course, the expected result is the nursery rhyme "Row, Row, Row Your Boat"

https://en.wikipedia.org/wiki/Row,_Row,_Row_Your_Boat

Hints

In order to "Rest for 1 second", look up the sleep() function. It is available in the unistd.h library, and provides a way to suspend a process for a specified number of seconds.

Explanation / Answer

#include <pthread.h> #include <stdio.h> #include <stdlib.h> void *worker_thread(void *arg) { printf("This is worker_thread() "); pthread_exit(NULL); } int main() { pthread_t my_thread; int ret; printf("In main: creating thread "); ret = pthread_create(&my_thread, NULL, &worker_thread, NULL); if(ret != 0) { printf("Error: pthread_create() failed "); exit(EXIT_FAILURE); } pthread_exit(NULL); } - See more at: http://www.bogotobogo.com/cplusplus/multithreading_pthread.php#sthash.Dln7DVKv.dpuf