c++ - boost asio stream_descriptor 和 event fd 线程安全

标签 c++ multithreading boost boost-asio

eventfd 根据 man pages线程安全的 属性部分

ATTRIBUTES top
For an explanation of the terms used in this section, see
attributes(7).

   ┌──────────┬───────────────┬─────────┐
   │Interface │ Attribute     │ Value   │
   ├──────────┼───────────────┼─────────┤
   │eventfd() │ Thread safety │ MT-Safe │

我想用 boost::asio::posix::stream_descriptor 包装 eventfd 这样我就可以在 boost::asio 中使用它了: :io_service

根据boost stream_descriptor reference , stream_descriptor 不是线程安全的

Thread Safety
Distinct objects: Safe.
Shared objects: Unsafe.

因此,如果我理解正确,使用 boost::asioasync_read_some/write_some 进行多线程读取/写入是不安全的/到用 stream_descriptor 包裹的 eventfd

这是一种“降级”,因为原生 eventfd 允许它。

我的理解正确吗?

最佳答案

确实。


一些精度:

  • 线程安全适用于 eventfd 调用,而不是 fd
  • 无论如何,fd 都是线程安全的,您可以自由地对它们进行系统调用
  • 这当然不是“降级”,因为您仍然可以像以前一样使用 fd(没有人强制您使用非线程安全对象)

没有什么能阻止您创建绑定(bind)到同一个 fd 的两个实例。 只需使用 release() 即可避免(双重)关闭。

这里有一个类似/相关的答案:How to avoid data race with `asio::ip::tcp::iostream`?

关于c++ - boost asio stream_descriptor 和 event fd 线程安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48297084/

相关文章:

javascript - JavaScript 中是否有等效的 Task.Run ?

c++ - 从目录中提取以特定单词开头的所有文件

c++ - Boost arm交叉编译失败

c++ - boost Date_Time 日期解析不起作用

c++ - 创建 Task then 函数(如 std::future.then)

javascript - 调用window.setInterval会在chrome中打开一个新线程吗?

C# TCP 聊天应用线程

c++ - 出现 "ISO C++ forbids declaration ..."错误。?

c++ - 在 OpenCV 中绘制多边形?

c++ - ld : symbol(s) not found for architecture x86_64 (Xcode 4)//opengles. o