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::asio
的 async_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/