Cross-platform way of yielding a thread in C/C++?

孤人 提交于 2019-12-01 16:19:16

问题


In C and C++ is there a cross-platform way of yielding a thread? Something like sched_yield() or Sleep(0)? Does SDL_Delay(0) always yield or will it return immediately in some implementations?


回答1:


Given that neither C nor C++ (up to C++98) has "threads," there is no fully cross-platform way for a thread to yield.

In C++0x, there is a function std::this_thread::yield() that can be called to yield. That will be the portable way for a thread to yield, once people start using the C++0x threads library.




回答2:


in the c++ case, boost::thread::yield() does what you ask. On platforms with posix threads, pthread_yield() performs the same function for C and anything that links with it. On platforms where this doesn't immediately stop the thread and start another, it's because the scheduler doesn't support that functionality. I don't think that many such platforms actually exist in the wild.



来源:https://stackoverflow.com/questions/4282773/cross-platform-way-of-yielding-a-thread-in-c-c

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