在 C/C 中生成线程的跨平台方式++?
在 C 和 C++ 中是否有跨平台的方式产生线程? 像 sched_yield() 或 Sleep(0) 这样的东西? 在某些实现中,SDL_Delay(0) 是否总是产生或会立即返回?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
鉴于 C 和 C++(直到 C++98)都没有“线程”,因此没有完全跨平台的方式让线程产生。
在C++0x中,有一个函数
std::this_thread::yield()
可以调用yield。一旦人们开始使用 C++0x 线程库,这将是线程让出的可移植方式。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.在 C++ 情况下,
boost::thread::yield()
执行您所要求的操作。在具有 posix 线程的平台上,pthread_yield( )
对 C 以及与其链接的任何内容执行相同的功能。在平台上,这不会立即停止线程并启动另一个线程,这是因为调度程序不支持该功能。我认为这样的平台实际上并不存在。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.