I got the following scenario with a concurrency problem, implemented using pthread library:
I got a thread that might be cancelled at any time. When that thread is cancelled, it needs to cancel its child thread, and make sure its child thread is already cancelled before it terminates.
So I end up with calling pthread_join twice on the child thread, once in the thread routine (as when the thread is not cancelled, I need that result), once in the thread's cancellation cleanup handler.
However, pthread_join doesn't allow joining the same thread twice, so what will happen?
Below is pseudo code:
void CleanupFunc(void* ChildThread)
{
pthread_cancel(*(pthread_t*)ChildThread);
pthread_join(*(pthread_t*)ChildThread, NULL);
}
void* ThreadFunc(void* _)
{
pthread_t ChildThread;
pthread_cleanup_push(&CleanupFunc, &ChildThread);
pthread_create(&ChildThread, NULL, &ChildThreadFunc, NULL);
pthread_join(ChildThread, NULL);
pthread_cleanup_pop(0);
}
Thanks in advance!