简体   繁体   中英

eventfd_read/write versus sem_wait/post

On Linux, in a C/C++ program, if I do not care about my eventfd being used in a "select", is it better to use eventfd_read/write (with EFD_SEMAPHORE flag) or sem_wait/post?

  • Are there any performance, reliability, portability issues?
  • As my program uses some other eventfd objects (with "select"), I think it would be more consistent to use eventfd than sem_wait/post.

sem_wait / sem_post are entirely userspace except when sem_wait blocks or sem_post posts to a semaphore that has a waiter. Even then, the syscalls they perform are some of the fastest paths in the kernel.

On the other hand, anything using file descriptors and io for synchronization is full of syscalls, and they're some of the slowest paths in the kernel due to the enormous complexity of io.

If you don't need select and you're writing multithreaded or multi-processes code anyway, I think the choice to use semaphores instead of eventfd is a no-brainer (ie the obvious choice, for those unfamiliar with the slang).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM