#include <pthread.h>
#include <cstdio>
void *printa(void *) {
printf("a");
return NULL;
}
void *printb(void *) {
printf("b");
return NULL;
}
int main() {
pthread_t pa, pb;
pthread_create(&pa, NULL, printa, NULL);
pthread_create(&pb, NULL, printb, NULL);
for(;;);
}
I expect it to print "a" and "b" in any order, but it can run and exit without print anything. Why? ADD: So the reason is the main function exit before the threads was run. So I add a for(;;); in the end of main, and it seems that "a" and "b" is never printed.