Is std::ifstream thread-safe & lock-free?

后端 未结 3 1743
小鲜肉
小鲜肉 2020-12-18 09:17

I intend to perform opening for reading a single file from many threads using std::ifstream. My concern is if std::ifstream is thread-safe & lock-free?

More deta

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-18 09:56

    See http://gcc.gnu.org/onlinedocs/libstdc++/manual/using_concurrency.html.

    As of the writing of that manual page, GCC's standard library defers to the operating system's C stdio file buffering. They avoid keeping state outside the C FILE structure and achieve some level of safety through it.

    Since the C stdio library implements a buffer of a single range within the file around the last I/O operation, I don't see how a lock-free implementation is possible. The operations on a file must be processed serially. Perhaps unbuffered mode could help; that's a little more research than I'd like to do right now.

提交回复
热议问题