I am learning how to use <pthread.h>
, and the textbook says that:
The
pthread_join
function blocks the calling thread ...
But in my test, it didn't:
void *a_thread(void *pt) {
puts("a_thread:join");
pthread_join(*(pthread_t *)pt, NULL); // <-- a_thread didn't wait for
puts("a_thread:joined"); // itself to finish executing.
return NULL;
}
int main(void) {
pthread_t t;
puts("create");
pthread_create(&t, NULL, a_thread, &t);
puts("created");
puts("main:join");
pthread_join(t, NULL);
puts("main:joined");
}
gcc -Og -o test test.c; ./test
:
create
created
main:join
a_thread:join
a_thread:joined
main:joined
From the pthread_join
man page:
If multiple threads simultaneously try to join with the same thread, the results are undefined.
Here you have the worker thread and the main thread both trying to join on the worker thread, sounds like UB to me.