Are there any equivalents to the futex in Linux/Unix?

前端 未结 2 1214
梦如初夏
梦如初夏 2021-02-04 09:21

I\'m looking for something could be used for polling (like select, kqueue, epoll i.e. not busy polling) in C/C++. In oth

2条回答
  •  佛祖请我去吃肉
    2021-02-04 09:44

    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:

    1. user-mode synchronisation of the count (with a CAS or similar)
    2. kernel management of the wait queue and thread sleep/wait

    I assume the mutex + condvar overhead you're worried about is the cond_wait->re-lock->unlock sequence, which is indeed avoided here.

提交回复
热议问题