How to synchronize threads at regular intervals?

Viewed 97

I have problems synchronizing several threads at regular intervals without consuming too much cpu while waiting.

I have a main thread, and several calculations threads that are all in the form :

CalcThread()   // x N threads
{
  loop{
    - do some calc stuff (variable but finite duration).
    - wait until main thread give a "continue" signal.
  }
}

MainThread()
{
  loop{
    - wait for all calc threads to be in waiting state.
    - do some calc synthesis stuff.
    - send a "continue" signal to calc threads.
  }
}

For the moment I make my threads waiting for each others looping the std::this_thread::yield() instruction with some condition on atomic shared flags. It works, BUT these loops are very CPU consuming.

There must be another solution, probably using mutex and condition_variable, but I am new to this, and all my attempt leads to failure...

Does anyone have an idea ? Thanks for your help.

1 Answers
Related