0

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.

griffin
  • 1,261
  • 8
  • 24
  • Why is it a separate thread if the other has to wait for it? – Bart Friederichs Aug 08 '13 at 15:05
  • I would suggest you do all initialisation in the main thread before you spawn your second. There is no point in having two if they are just waiting for one another. – Sergey L. Aug 08 '13 at 15:32
  • As stated in the question, I don't have control over the spawning process. – griffin Aug 12 '13 at 09:10
  • ① What threading API are you using? ② Are you able to statically initialize globals for your two threads to use? ③ Can thread A spawn thread B, rather than letting the main program spawn both? – pilcrow Aug 12 '13 at 13:42
  • 1) don't know, as I don't control the spawning process 2) no, but I can share data between the threads via a function call which returns a pointer to a sharable pointer, and that sharable pointer should be NULL on start 3) no, as I don't control the main program – griffin Aug 12 '13 at 13:45

2 Answers2

2

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.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • How would I accomplish this in my scenario? Problem is the execution order of the two threads can be random from my point of view, and I can't execute anything else beforehand, so I can't init any kinf of variable or the like, or am I missing something? – griffin Aug 12 '13 at 09:13
  • Then you need to rethink your solution, so that you CAN execute something beforehand. It's not possible to solve all problems by arbitrarily badly designing something, and then asking on SO for a solution to the little part that is apparently wrong, and expect to get a an answer that solves that without having to redesign something. – Mats Petersson Aug 12 '13 at 09:18
  • The thing is it isn't my design - it's a server software which executes c scripts as servlets. Nevertheless I'm about to solve this "impossible" problem by myself, so it isn't that impossible after all it seems ... – griffin Aug 12 '13 at 09:55
  • Well, if you have a dependency between two lumps of code that are run in separate threads, and there isn't a way to create a synchronisation object before the threads are started, then it's a bad design. I didn't say it's impossible, just that if you ask for "how do I solve this", then I will suggest some solution that I know will work. Please do share your solution, as I'm curious as to how you will do it. I guess you can create a file in the filesystem or or something like that... – Mats Petersson Aug 12 '13 at 10:05
  • Your answer and comment lead me to add some information to the question (just edited/updated it), thank you! (upvoted your answer for that, as it seems to also point to the right direction, though I still don't get why I should actually prefer a conditional variable over a simple mutex ...) – griffin Aug 12 '13 at 10:20
  • A mutex would work too. It's probably what is behind the pthread_condtion variables anyway - condition variables are more portable. And of course, if you have a variable that comes with the thread start, then you can use that to store a data block that contains a suitable sync object. – Mats Petersson Aug 12 '13 at 10:28
  • How should a condition variable be more portable than a mutex, when condition variables explicitely rely on being passed a mutex? Also, afaik cond_wait does unlock mutex -> check condition -> lock mutex. That's why you have to pass a locked mutex, otherwise it's undefined behaviour. – griffin Aug 12 '13 at 13:54
  • 1
    It is more portable than using a NATIVE Mutex, such as the ones in Windows. But if you are using a pthread_mutex, then it's the same portability. And of course C++11 has mutex and thread support. – Mats Petersson Aug 12 '13 at 13:57
0

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.

Lidong Guo
  • 2,817
  • 2
  • 19
  • 31
  • 1. fork doesn't create a thread but a child process. Also I don't have control over the spawning process as stated in the question. 2. how would a solution using signal look like, asuming random order of execution of the two threads? – griffin Aug 12 '13 at 09:12