Considering following situation:
2 threads, where 1 should initialize some data, which takes an indeterministic amount of time, and the other thread needs to wait for that.
Problems I have:
How can I accomplish this?
Edit: A probably not so unimportant thing I (unfortunately) left out from the initial question: I have access to some pointers which can be accessed from all threads and should be NULL at start, so this should probably help with synchronization.
Edit 2: Through experimenting I found out that "the other thread" actually always is spawned and a function is called and needs to return before the main thread starts. Unfortunately I have to wait at that point for the main thread to finish it's work, which makes this whole thing impossible. So the question can from now on be considered theoretically (and hopefully practical again, as soon as the provider of the software changes that or provides a way for this to work)
Also added g-wan (web server executing c scripts as servlets/handler/maintenance script) tag - initially I didn't want to add this tag as the question is not specific to the software, but as it seems it might help to understand the "why" as well as circumstances ; "main" thread in my case is the maintenance script, and "other thread" is a connection handler.
My problem has been "solved" with support/help from the software vendor, so I will accept the pthread_cond_wait answer at this point, as it is generally correct as far as I can say.
Assuming you are using pthread
, then you can use pthread_cond_wait
to synchronise threads. When the first thread has completed it's task, signal the condition variable, and the second thread should be using the pthread_cond_wait
to wait for the same condition variable.