开发者

Is there a way to make sem_wait not spin?

开发者 https://www.devze.com 2023-02-23 02:13 出处:网络
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 th

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


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 to WaitForSingleObject, and pthread_cond_signal is analogous to SetEvent.

0

精彩评论

暂无评论...
验证码 换一张
取 消