I have a multi-threaded program and cannot figure out why the printf does not work as expected.
This is my code:
#include <pthread.h>
#include <stdio.h>
void *Msg(void *arg)
{
pthread_t x;
x= pthread_self();
printf("x=%ld\n", x);
printf("This MSG from a thread \n");
pthread_exit((void*)0);
}
int main()
{
pthread_t n;
pthread_create(&n, NULL, Msg, NULL);
pthread_create(&n, NULL, Msg, NULL);
printf("Mother thread\n");
return 0;
}
My question is why it doesn’t printf the sentence “This MSG…”.
You should join the threads to give them a chance to run before the main thread exits. When one thread exits the process all the other threads are killed.
Try: