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
.
精彩评论