有没有办法让 sem_wait 不旋转?
我目前正在开发跨平台任务调度程序,但在等待信号量时遇到 sem_wait 旋转的问题。在 Windows 上,我使用 WaitForSingleObject,它在等待时生成线程,这正是我想要的。但是 sem_wait 会导致线程旋转,这是低效且不必要的。有没有办法让 sem_wait 产生线程而不是仅仅旋转?
谢谢
I'm currently working on a cross-platform task scheduler, but I'm having a problem with sem_wait spinning while waiting for the semaphore. On Windows, I'm using WaitForSingleObject, which yields the thread while it waits, which is what I want. But sem_wait causes the threads to just spin, which is inefficient and unnecessary. Is there any way to make sem_wait yield the thread instead of just spinning?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试使用条件变量而不是信号量。它们的功能并不完全相同,但根据您的使用情况,您也许可以使用它们。它们的功能与 Windows 事件类似,即您等待条件变量以等待某些事情发生,并向条件变量发出信号以指示发生了某些事情。
pthread_cond_wait
类似于WaitForSingleObject
,pthread_cond_signal
类似于SetEvent
。You could try using condition variables instead of semaphores. They're not completely identical in function, but depending on your usage, you might be able to use instead. They function similarly to Windows events, in that you wait on a condition variable to wait for something to happen, and you signal a condition variable to indicate that something happened.
pthread_cond_wait
is analogous toWaitForSingleObject
, andpthread_cond_signal
is analogous toSetEvent
.