Question

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:

  • I can't do anything before any of the two threads are running, as I don't have control over the spawning process (C code is compiled and executed in a script like fashion).
  • I don't know if the init thread reaches any point in code before the other thread, as they are started right after each other, and from that one execution order becomes indeterministic from my point of view
  • I need to make sure that the init portion of the main thread runs first (e.g. the other thread should not try to use uninitialized)
  • I want to clean up whatever I use for this synchronization afterwards, as this is a one time thing in a long running process, where even very small savings on memory count. E.g. mutex/semaphore should be destroyed afterwards.

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.

Was it helpful?

Solution

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.

OTHER TIPS

1.Maybe you can use the other thread as parent thread and it use fork to create the init thread .The parent use wait or waitpid to wait the init thread.

2.If the are just two thread and you can use signal .Like the after the init thread finish .it send a signal while other thread block itself and waiting for the signal .Once it received the signal ,it do clean jobs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top