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

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);

}

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 Databases Questions!