Charlie Richards
Charlie Richards

Reputation: 11

Synchronization streams in a specific queue using Mutex

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: 0

Views: 217

Answers (1)

superM
superM

Reputation: 8695

You should keep a variable set to 0. When the first thread runs it sets the varialbe to 1. The second thread waits before the variable is set to 1, sets it to 2 and starts working. And the same for the third one. Use mutex to synchronize checking/setting the variable. Also preferabley use timeout or events for checking the variable.

Upvotes: 1

Related Questions