I'm looking for something could be used for polling (like select
, kqueue
, epoll
i.e. not busy polling) in C/C++. In other word, I need to block a thread, and then wake it up in another thread with as little overhead as possible.
A mutex
+ condition variable
works, but there is a lot of overhead. A futex
also works, but that's for Linux only (or maybe not?). Extra synchronization is not required as long as the polling itself works properly, e.g. no race when I call wait
and wake
in two threads.
Edit: If such a "facility" doesn't exist in FreeBSD, how to create one with C++11 built-in types and system calls?
Edit2: Since this question is migrated to SO, I'd like to make it more general (not for FreeBSD only)
semaphores are not mutexes, and would work with slightly less overhead (avoiding the mutex+condvar re-lock, for example)
Note that since any solution where a thread sleeps until woken will involve a kernel syscall, it still isn't cheap. Assuming x86_64 glibc and the FreeBSD libc are both reasonable implementations, the unavoidable cost seems to be:
I assume the mutex + condvar overhead you're worried about is the cond_wait->re-lock->unlock sequence, which is indeed avoided here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With