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

Part 3: Pthread Mutex Implementation (10 points) Modify the pthread-data-sharing

ID: 3753667 • Letter: P

Question

Part 3: Pthread Mutex Implementation (10 points)

Modify the pthread-data-sharing-mutex-os-call.cpp program to apply a Pthread mutex solution, i.e., you will use Linux system calls to control access to the critical region.

Refer to the prep materials for background info (section 2.3.6 in the textbook) and also to the Linux manual for the names and parameters of the functions. See https://linux.die.net/man/3/pthread_mutex_init

The necessary changes will be very small, i.e., not a lot of code is needed.

Build and execute the updated program several times.

Expected Output:
Your program should produce output similar to the following (Note: the order of your threads may be different)

Base Code:

#include <iostream>

#include <pthread.h>

#include <stdlib.h>

#define TOTAL_THREADS 4

int count;

pthread_mutex_t the_mutex; // phread mutex variable

void* myFunction(void* arg)

{

int actual_arg = *((int*) arg);

  

for(unsigned int i = 0; i < 10; ++i) {

  

// TODO:

// Use a Pthread mutex to control

// access to the critical region.

  

// Beginning of the critical region

  

count++;

std::cout << "Thread #" << actual_arg << " count = " << count << std::endl;

  

// Random wait - This code is just to ensure that the threads

// show data sharing problems

int max = rand() % 100000;

  

for (int x = 0; x < max; x++);

  

// End of random wait code

  

  

// End of the critical region

  

  

}

  

pthread_exit(NULL);

}

int main()

{

int rc[TOTAL_THREADS];

pthread_t ids[TOTAL_THREADS];

int args[TOTAL_THREADS];

  

  

// TODO: Initialize the pthread mutex here

  

  

count = 0;

for(unsigned int i = 0; i < TOTAL_THREADS; ++i) {

args[i] = i;

rc[i] = pthread_create(&ids[i], NULL, myFunction, (void*) &args[i]);

}

  

for(unsigned int i = 0; i < TOTAL_THREADS; ++i) {

pthread_join(ids[i], NULL);

}

  

std::cout << "Final count = " << count << std::endl;

pthread_exit(NULL);

}

Thread #0 count 1 Thread #0 count 2 Thread #0 count 3 Thread #0 count-4 Thread #0 count 5 Thread #0 count 6 Thread #0 count 7 Thread #0 count 8 Thread #0 count 9 Thread #0 count- 10 Thread #3 count 11 Thread #3 count- 12 Thread #3 count 13 Thread #3 count 14 Thread #3 count 15 Thread #3 count- 16 Thread #3 count 17 Thread #3 count- 18 Thread #3 count 19 Thread #3 count 20 Thread #2 count 21 Thread #1 count-22 Thread #1 count-23 Thread #1 count-24 Thread #1 count 25 Thread #1 count 26 Thread #1 count-27 Thread #1 count 28 Thread #1 count 29 Thread #1 count-30 Thread #1 count 31 Thread #2 count 32 Thread #2 count -33 Thread #2 count 34 Thread #2 count 35 Thread #2 count-36 Thread #2 count 37 Thread #2 count 38 Thread #2 count 39 Thread #2 count-40 Final count 40

Explanation / Answer

#include <iostream>
#include <pthread.h>
#include <stdlib.h>
#define TOTAL_THREADS 4

// Sharable resources
int count;
// phread mutex variable
pthread_mutex_t the_mutex;

// Function to share the count sharable by multiple threads
void* myFunction(void* arg)
{
// Converts the void pointer data to integer pointer
int actual_arg = *((int*) arg);

// Loops 10 times
for(unsigned int i = 0; i < 10; ++i)
{
// Uses a Pthread mutex to control access to the critical region.
pthread_mutex_lock(&the_mutex);
// Beginning of the critical region
count++;
std::cout << "Thread #" << actual_arg << " count = " << count << std::endl;

// Random wait - This code is just to ensure that the threads show data sharing problems
int max = rand() % 100000;

// Loops up to random max times
for (int x = 0; x < max; x++);
// End of random wait code

// End of the critical region
pthread_mutex_unlock(&the_mutex);
}// End of for loop
// Exit the thread
pthread_exit(NULL);
}// End of function

// main function definition
int main()
{

int rc[TOTAL_THREADS];
pthread_t ids[TOTAL_THREADS];
int args[TOTAL_THREADS];

// Initialize the pthread mutex here
count = 0;
// Checks the thread initialization
if (pthread_mutex_init(&the_mutex, NULL) != 0)
{
std::cout<<" mutex init has failed ";
return 1;
}// End of if condition

// Loops through total number of threads
for(unsigned int i = 0; i < TOTAL_THREADS; ++i)
{
// Assigns loop variable value to args i index position
args[i] = i;
// Stores the return value
// Creates a thread
rc[i] = pthread_create(&ids[i], NULL, myFunction, (void*) &args[i]);
}// End of for loop

// Loops through total number of threads
for(unsigned int i = 0; i < TOTAL_THREADS; ++i)
{
// Waiting for thread to finish
pthread_join(ids[i], NULL);
}// End of for loop

// Destroy the thread
pthread_mutex_destroy(&the_mutex);
// Displays the final count
std::cout << "Final count = " << count << std::endl;
pthread_exit(NULL);
}// End of main function

Sample Output:

Thread #0 count = 1
Thread #1 count = 2
Thread #2 count = 3
Thread #3 count = 4
Thread #0 count = 5
Thread #1 count = 6
Thread #2 count = 7
Thread #3 count = 8
Thread #0 count = 9
Thread #1 count = 10
Thread #2 count = 11
Thread #3 count = 12
Thread #0 count = 13
Thread #1 count = 14
Thread #2 count = 15
Thread #3 count = 16
Thread #0 count = 17
Thread #1 count = 18
Thread #2 count = 19
Thread #3 count = 20
Thread #0 count = 21
Thread #1 count = 22
Thread #2 count = 23
Thread #3 count = 24
Thread #0 count = 25
Thread #1 count = 26
Thread #2 count = 27
Thread #3 count = 28
Thread #0 count = 29
Thread #1 count = 30
Thread #2 count = 31
Thread #3 count = 32
Thread #0 count = 33
Thread #1 count = 34
Thread #2 count = 35
Thread #3 count = 36
Thread #0 count = 37
Thread #1 count = 38
Thread #2 count = 39
Thread #3 count = 40
Final count = 40