I want to halt one thread till another thread has finished initializing without using pthread_join. I tried using a join but it leads to a deadlock due to some asynchronous inter-thread communication system that we have. Right now, I’m using a (custom) lock to achieve this.
In Thread 1:
lock_OfflineWorker.Lock() if (pthread_create(&tid0, NULL, RunOfflineWorker, NULL) != 0) { } lock_OfflineWorker.TryLock(); lock_OfflineWorker.Unlock();
In Thread 2:
bool OfflineWorker::Initialize() { lock_OfflineWorker.Unlock(); }
But this is inelegant and I’m not too sure about the side effects (possibility for another deadlock). Is this Ok? If not, is there another way to achieve this (using locks or otherwise)
EDIT: Forgot include the ‘RunOfflineWorker’ function
void* RunOfflineWorker(void* pData) { g_OfflineWorker.Initialize(); }
You can use a pthread condition to wait until the job reaches the wanted state.
The thread1 waits with
pthread_cond_wait()and the thread2 signals it withpthread_cond_signal().You need :
The first thread inits all :
Then it waits with the mutex locked. Usually you put the wait in a loop to check the condition whatever it is.
The other thread does this when appropriate :