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

check the code,and fix the solution that the pthread_join is not working as inte

ID: 3919166 • Letter: C

Question

 check the code,and fix the solution that the pthread_join is not working as intended?   #include <stdio.h>       /* standard I/O routines                 */ #include <pthread.h>     /* pthread functions and data structures */  void* PrintHello(void* data) {     pthread_t tid = (pthread_t)data;    /* data received by thread */      pthread_join(tid, NULL);            /* wait for thread tid     */     printf("Hello from new thread %u - got %u ", pthread_self(), data);     pthread_exit(NULL);                 /* terminate the thread    */ }  /* like any C program, program's execution begins in main */ int main(int argc, char* argv[]) {     int        rc;                     /* return value                  */     pthread_t  thread_id;              /* thread's ID (just an integer) */     int        tid;      tid = pthread_self();      rc = pthread_create(&thread_id, NULL, PrintHello, (void*)tid);       if(rc)                             /* could not create thread */     {         printf("  ERROR: return code from pthread_create is %d  ", rc);         exit(1);     }     sleep(1);     printf("  Created new thread (%u) ...  ", thread_id);     pthread_exit(NULL);          }

Explanation / Answer

The only error that I got during compiling was 'The undefined refrence to sleep()' on line number 29.

So, to avoid that error, you can do the following thing.

if you are running under the Windows operating system( as I'm using), include windows.h header file to your program and change sleep() to Sleep().

The problem would get disappeared and you will get the desired output.

I hope this helps.

THE OUTPUT:-

Created new thread (2) ...

Hello from new thread 2 - got 1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote