Eventfd_read / write versus sem_wait / post

On Linux, in a C / C ++ program, if I don't need my eventfd to be used in "select", is it better to use eventfd_read / write (with the EFD_SEMAPHORE flag) or sem_wait / post?

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

source to share

1 answer


/ sem_post

is completely user space, except when sem_wait

blocking or sem_post

messages to a semaphore that has a waiter. Even then, these system calls are some of the fastest paths in the kernel.

On the other hand, anything that uses file descriptors and io for synchronization is full of system calls, and they are some of the slowest paths in the kernel due to the sheer complexity of io.

If you don't need a choice and are writing multi-threaded or multi-processor code anyway, I think the choice to use semaphores instead of eventfd is not a problem (i.e. an obvious choice for those unfamiliar with slang).



All Articles