Do mutex locks happen in the same order they are asked?

后端 未结 1 1724
死守一世寂寞
死守一世寂寞 2021-01-13 11:49

I am currently trying to create a very simple thread pool using std::thread. In order to maintain threads \'alive\' after their given task is done, I associate

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-13 12:06

    No, you cannot guarantee that your thread loop will ever acquire the lock with your example as is. Use a conditional variable to signal to the thread loop that it should awake and take the lock. See std::condition_variable::wait(...). condition-variable

    More on this topic in general can be found here http://en.wikipedia.org/wiki/Condition_variable. If you were using the pthread library, the equivalent call would be pthread_cond_wait in your "Thread loop" and pthread_cond_signal in your runTask function.

    0 讨论(0)
提交回复
热议问题