Question: Task 1 . Producer Consumer Threads This task is intended to provide a solution to the Producer Consumer problem using threads, mutex, and a condition

Task 1. Producer Consumer Threads
This task is intended to provide a solution to the Producer Consumer problem using threads, mutex, and a condition variable. Reference Figure 2-32 in the textbook. Note that the code in Figure 2-32 is written in C, but your program can be developed in C++.
Instructions
Develop a solution to the Producer Consumer problem using threads, mutex, and a condition variable. The program will Produce and Consume 10 items. Create two threads, one for the Producer and one for the Consumer
Use a mutex and condition variable to prevent the race condition and enable synchronization so that only one thread updates the buffer at any given time
For the output, display the buffer value in the Producer and Consumer threads whenever it is changed.
Include the header for the C++ thread class.
Include the header for the C++ mutex class.
Include the header for the C++ condition_variable class.
Note that you will need to compile with the -pthread option to link with the pthread library. For example:
$ gcc -o pthread -pthread pcthread.c
$ g++-o pcthread -pthread pcthread.cpp
Sample output:
Threads started ...
In producer: buffer =1
In consumer: buffer =1
In producer: buffer =2
In consumer: buffer =2
In producer: buffer =3
In consumer: buffer =3
In producer: buffer =4
In consumer: buffer =4
In producer: buffer =5
In consumer: buffer =5
In producer: buffer =6
In consumer: buffer =6
In producer: buffer =7
In consumer: buffer =7
In producer: buffer =8
In consumer: buffer =8
In producer: buffer =9
In consumer: buffer =9
In producer: buffer =10
In consumer: buffer =10
Ending main(): buffer =0
Task 2. Locking and Unlocking a Mutex
Modify the C program given below so that two created threads can avoid race conditions using mutual exclusion.
thread_incr.c:
#include
#include
#include
#include
#include
#include
static volatile int glob =0;
static void */* Loop 'arg' times incrementing 'glob' */
threadFunc(void *arg){
int loops =*((int *) arg);
int loc, j;
for (j =0; j < loops; j++){
loc = glob;
loc++;
glob = loc;
}
return NULL;
}
int main(int argc, char *argv[]){
pthread_t t1, t2;
int loops, s;
loops =(argc >1)? atoi(argv[1]) : 10000000;
s = pthread_create(&t1, NULL, threadFunc, &loops);
if (s !=0)
printf("pthread_create");
s = pthread_create(&t2, NULL, threadFunc, &loops);
if (s !=0)
printf("pthread_create");
s = pthread_join(t1, NULL);
if (s !=0)
printf("pthread_join");
s = pthread_join(t2, NULL);
if (s !=0)
printf("pthread_join");
printf("glob =%d
", glob);
exit(EXIT_SUCCESS);
}
Use following two POSIX thread functions:
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
Compile and run your code (Note that you will need to compile with the -pthread option to link with the pthread library.):
$ gcc -o thread_incr -pthread thread_incr.c
$ ./thread_incr 10000000
When you run your code, it should show:
$ glob =20000000
Submit followings in the Canvas:
1. Task 1: pcthread_your_name.c (or pcthread_your_name.cpp)
2. Task 2: thread_incr_your_name.c (or thread_incr_your_name.cpp)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!