Write a C++ program with give information #include <pthread.h> #include <stdio.h
ID: 3911465 • Letter: W
Question
Write a C++ program with give information #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> /* ** Compile and run this program, and make sure you get the 'aargh' error ** message. Fix it using a pthread mutex. The one command-line argument is ** the number of times to loop. Here are some suggested initial values, but ** you might have to tune them to your machine. ** Debian 8: 100000000 ** Gouda: 10000000 ** OS X: 100000 ** You will need to compile your program with a "-lpthread" option. */ #define NUM_THREADS 2 int i; void *foo (void *bar) { pthread_t *me = new pthread_t (pthread_self()); printf("in a foo thread, ID %ld ", *me); for (i = 0; i < *((int *) bar); i++) { int tmp = i; if (tmp != i) { printf ("aargh: %d != %d ", tmp, i); } } pthread_exit (me); } int main(int argc, char **argv) { int iterations = strtol(argv[1], NULL, 10); pthread_t threads[NUM_THREADS]; for (int i = 0; i < NUM_THREADS; i++) { if (pthread_create(&threads[i], NULL, foo, (void *) &iterations)) { perror ("pthread_create"); return (1); } } for (int i = 0; i < NUM_THREADS; i++) { void *status; if (pthread_join (threads[i], &status)) { perror ("pthread_join"); return (1); } printf("joined a foo thread, number %ld ", *((pthread_t *) status)); } return (0); }
Explanation / Answer
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/*
** Compile and run this program, and make sure you get the 'aargh' error
** message. Fix it using a pthread mutex. The one command-line argument is
** the number of times to loop. Here are some suggested initial values, but
** you might have to tune them to your machine.
** Debian 8: 100000000
** Gouda: 10000000
** OS X: 100000
** You will need to compile your program with a "-lpthread" option.
*/
#define NUM_THREADS 2
int i;
// creating a thread mutex for synchronization
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
void *foo (void *bar)
{
pthread_t *me = new pthread_t (pthread_self());
printf("in a foo thread, ID %ld ", *me);
for (i = 0; i < *((int *) bar); i++)
{
int tmp = i;
// Checking if the lock is thread is not locked, error message is printed
if (pthread_mutex_trylock(&lock) != 0)
{
printf ("aargh: %d != %d ", tmp, i);
}
}
pthread_mutex_unlock (&lock);
pthread_exit (me);
}
int main(int argc, char **argv)
{
int iterations = strtol(argv[1], NULL, 10);
pthread_t threads[NUM_THREADS];
for (int i = 0; i < NUM_THREADS; i++)
{
if (pthread_create(&threads[i], NULL, foo, (void *) &iterations))
{
perror ("pthread_create");
return (1);
}
}
for (int i = 0; i < NUM_THREADS; i++)
{
void *status;
if (pthread_join (threads[i], &status))
{
perror ("pthread_join");
return (1);
}
printf("joined a foo thread, number %ld ", *((pthread_t *) status));
}
return (0);
}
OUPUT
in a foo thread, ID 139699783497472
aargh: 1 != 1
aargh: 2 != 2
aargh: 3 != 3
aargh: 4 != 4
in a foo thread, ID 139699791890176
aargh: 1 != 1
aargh: 2 != 2
aargh: 3 != 3
aargh: 4 != 4
joined a foo thread, number 139699791890176
joined a foo thread, number 139699783497472
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.