Reputation: 307
there is a slight problem with threads, please tell me how to solve it or at least in which direction to look for, looking for a long time, but so far without success ... Here's the problem: there are 3 threads, they can run in any order (321;213;123...). In whatever order flows we would not run, they need to work out in the order 1,2,3. For this purpose, use thread synchronization with ONE mutex. In advance thank you.
Upvotes: 1
Views: 166
Reputation: 5344
use mutex to protect a global integer, iThread. Init iThread to 1. create threads, pass in an id for each thread: 1,2,3.
In thread proc, lock mutex to check iThread. If iThread != thread id, then wait on condition.
if iThread = thread id: unlock mutex, execute work in threadproc; at end of thread proc: lock mutex, ++iThread, and signal condition, unlock mutex.
something like that. There are a million other ways.
Upvotes: 1