Implement this in C++ using pthreads. (You\'ll have to use Cygwin or something s
ID: 3785183 • Letter: I
Question
Implement this in C++ using pthreads. (You'll have to use Cygwin or something similar if you're on Windows.)
You will have one shared variable that is modi ed called sum. In each thread, check a number and add it to sum if it is a factor of N (N is a random number).
You check to see if a number is a factor using the modulo operator. You'll need to protect access to sum using a mutex.
You'll also have a shared variable N, but no threads will modify it so it doesn't need to be protected.
Start with P = 1 to get things working. That way you won't have any race conditions.
You can cast an integer to a void* with the code (void*)(size t)N.
You can join a thread that has never been started and it will return immediately.
Please Use the following starter code to complete your answer
#include
#include
#include
int N;
int main(int argc, char **argv) {
if (argc < 3) {
printf("Usage: perfect N P ");
return 1;
}
N = atoi(argv[1]);
const int P = atoi(argv[2]);
printf("N = %d, P = %d ", N, P);
}
Explanation / Answer
#include <iostream>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
#include<stdio.h>
#include <errno.h>
//define MAX to hold number of thread
#define MAX 100
using namespace std;
int N;
void* thread_func(void *arg);
int sum;
int main(int argc, char **argv)
{
//We will store the thread IDs in an array
pthread_t thread[MAX];
if (argc < 3)
{
printf("Usage: perfect N P ");
return 1;
}
N = atoi(argv[1]);
const int P = atoi(argv[2]);
printf("N = %d, P = %d ", N, P);
//create P number of threads
for(int i = 0; i < P ; i++)
{
int err = pthread_create(&(thread[0]), NULL, &thread_func, (void*)&N);
if (err != 0)
printf(" can't create thread ");
else
printf(" Thread created successfully ");
}
//join threads
for(int i = 0; i < P ; i++)
{
pthread_join(thread[0],NULL);
}
printf("sum after executing all the thread = %d ",sum);
}
pthread_mutex_t count_mutex;
void* thread_func(void *arg)
{
//check if sum is factor of N
if( sum%N == 0)
{
//lock before adding to sum as sum is shared variable
pthread_mutex_lock(&count_mutex);
sum+=N;
//unloack after adding
printf("Adding %d Number to sum ",N);
printf("sum in thread = %d ",sum);
pthread_mutex_unlock(&count_mutex);
}
}
-----------------------------------------------------------------------------------------------------------------
//output
N = 5, P = 50
Thread created successfully
Thread created successfully
Adding 5 Number to sum
sum in thread = 5
Adding 5 Number to sum
sum in thread = 10
Thread created successfully
Adding 5 Number to sum
sum in thread = 15
Thread created successfully
Thread created successfully
Adding 5 Number to sum
sum in thread = 20
Adding 5 Number to sum
sum in thread = 25
Thread created successfully
Adding 5 Number to sum
sum in thread = 30
Thread created successfully
Adding 5 Number to sum
sum in thread = 35
Thread created successfully
Adding 5 Number to sum
sum in thread = 40
Thread created successfully
Adding 5 Number to sum
sum in thread = 45
Thread created successfully
Adding 5 Number to sum
sum in thread = 50
Thread created successfully
Adding 5 Number to sum
sum in thread = 55
Thread created successfully
Adding 5 Number to sum
sum in thread = 60
Thread created successfully
Adding 5 Number to sum
sum in thread = 65
Thread created successfully
Adding 5 Number to sum
sum in thread = 70
Thread created successfully
Adding 5 Number to sum
sum in thread = 75
Thread created successfully
Adding 5 Number to sum
sum in thread = 80
Thread created successfully
Adding 5 Number to sum
sum in thread = 85
Thread created successfully
Adding 5 Number to sum
sum in thread = 90
Thread created successfully
Adding 5 Number to sum
sum in thread = 95
Thread created successfully
Adding 5 Number to sum
sum in thread = 100
Thread created successfully
Adding 5 Number to sum
sum in thread = 105
Thread created successfully
Adding 5 Number to sum
sum in thread = 110
Thread created successfully
Adding 5 Number to sum
sum in thread = 115
Thread created successfully
Adding 5 Number to sum
sum in thread = 120
Thread created successfully
Adding 5 Number to sum
sum in thread = 125
Thread created successfully
Adding 5 Number to sum
sum in thread = 130
Thread created successfully
Adding 5 Number to sum
sum in thread = 135
Thread created successfully
Adding 5 Number to sum
sum in thread = 140
Thread created successfully
Adding 5 Number to sum
sum in thread = 145
Thread created successfully
Adding 5 Number to sum
sum in thread = 150
Thread created successfully
Adding 5 Number to sum
sum in thread = 155
Thread created successfully
Adding 5 Number to sum
sum in thread = 160
Thread created successfully
Adding 5 Number to sum
sum in thread = 165
Thread created successfully
Adding 5 Number to sum
sum in thread = 170
Thread created successfully
Adding 5 Number to sum
sum in thread = 175
Thread created successfully
Adding 5 Number to sum
sum in thread = 180
Thread created successfully
Adding 5 Number to sum
sum in thread = 185
Thread created successfully
Adding 5 Number to sum
sum in thread = 190
Thread created successfully
Adding 5 Number to sum
sum in thread = 195
Thread created successfully
Adding 5 Number to sum
sum in thread = 200
Thread created successfully
Adding 5 Number to sum
sum in thread = 205
Thread created successfully
Adding 5 Number to sum
sum in thread = 210
Thread created successfully
Adding 5 Number to sum
sum in thread = 215
Thread created successfully
Adding 5 Number to sum
sum in thread = 220
Thread created successfully
Adding 5 Number to sum
sum in thread = 225
Thread created successfully
Adding 5 Number to sum
sum in thread = 230
Thread created successfully
Adding 5 Number to sum
sum in thread = 235
Thread created successfully
Adding 5 Number to sum
sum in thread = 240
Thread created successfully
Adding 5 Number to sum
sum in thread = 245
Thread created successfully
Adding 5 Number to sum
sum in thread = 250
sum after executing all the thread = 250