std::this_thread::yield() usage?

佐手、 提交于 2020-01-14 07:57:25

问题


Can someone provide real-life example of std::this_thread::yield() usage in c++ application?


回答1:


I used yield in the implementation of std::lock, found here:

http://llvm.org/svn/llvm-project/libcxx/trunk/include/mutex

It turns out that when locking multiple locks/mutexes at a time, when you fail to get one, you can make the application faster by using yield prior to trying the locks/mutexes in a different order.

In this source code I'm actually calling sched_yield(). But that is only for the purpose of getting the header dependency the way I wanted it. On this platform std::this_thread::yield() is nothing more than a call to sched_yield():

http://llvm.org/svn/llvm-project/libcxx/trunk/include/thread



来源:https://stackoverflow.com/questions/5604059/stdthis-threadyield-usage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!