Which is the best way to yield thread?

Din*_*ray 0 c++ multithreading c++11

Program language: C++ 11
I use pipeline threads mode to deal data.
One thread generate data.
One thread process data.
While no data to deal, which is the best way to yield thread?
Now I use

std::this_thread::sleep_for(100ms); 
Run Code Online (Sandbox Code Playgroud)
  1. I wonder if there is a better way to yield?
  2. If sleep is well enough, how long time to sleep is better?

eer*_*ika 6

Which is the best way to yield thread?

It is std::this_thread::yield.

Now I use

std::this_thread::sleep_for(100ms);
Run Code Online (Sandbox Code Playgroud)

While sleeping does yield the thread as a side-effect, that's not all that it does. As the name implies, it blocks the thread for a given time.

However, it is unclear how yielding or sleeping would be useful in a producer / consumer case such as what you describe. What you probably should do is wait on a condition variable instead.