Using c fufill the requirements in the comments or fill in the blanks #include <
ID: 3598302 • Letter: U
Question
Using c fufill the requirements in the comments or fill in the blanks
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
void *PrintHello(void *threadid)
{
long tid;
tid = (long) threadid;
printf("Hello World! It's me, thread #%Id! ", tid);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
// create an array of thread struct instances with appropriate length
long t;
for(t=0; t<NUM_THREADS; t++){
printf("In main: creating thread %Id ", t);
//start a new thread and call the appropriate routine with. You need to handle errors.
//args of the routine should be cast as (void *)t
}
/* Last thing that main() should do */
pthread_exit(NULL);
}
Explanation / Answer
Below is your complete code
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
void *PrintHello(void *threadid)
{
long tid;
tid = (long) threadid;
printf("Hello World! It's me, thread #%Id! ", tid);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
// create an array of thread struct instances with appropriate length
pthread_t threads[NUM_THREADS];
long t;
int rc;
for(t=0; t<NUM_THREADS; t++){
printf("In main: creating thread %Id ", t);
//start a new thread and call the appropriate routine with. You need to handle errors.
//args of the routine should be cast as (void *)t
rc= pthread_create(&threads[t],NULL,PrintHello,(void *)t);
if(rc) {
printf("ERROR;Return code from pthread_create() is %d ",rc);
exit(-1);
}
}
/* Last thing that main() should do */
pthread_exit(NULL);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.