简体   繁体   English

eventfd_read / write与sem_wait / post

[英]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? 在Linux上,在C / C ++程序中,如果我不关心在“select”中使用我的eventfd,使用eventfd_read / write(使用EFD_SEMAPHORE标志)还是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. 由于我的程序使用了一些其他的eventfd对象(使用“select”),我认为使用eventfd比使用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. sem_wait / sem_post完全是用户空间,除非sem_wait阻塞或sem_post发布到有服务员的信号量。 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. 另一方面,使用文件描述符和io进行同步的任何东西都充满了系统调用,并且由于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). 如果您不需要选择并且您正在编写多线程或多进程代码,我认为使用信号量而不是eventfd的选择是一个明智的选择(对于那些不熟悉俚语的人来说,这是显而易见的选择)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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