流线程安全吗? [英] Is ofstream thread safe?

查看:795
本文介绍了流线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个程序,它使用多个 std :: ifstream s来读取二进制文件,一个 std :: ifstream 为每个线程。现在我需要知道,如果 std :: ofstream 在Windows和Linux上是线程安全的,用于在同一个文件中写入。我使用只使用一个 std :: ofstream 和使用多个线程。

我使用每个线程读取不同的块,并写这些块输出文件使用 seekp() write()。目前它是为我工作,但它是否有问题的大文件。

I am working on a program, which uses multiple std::ifstreams for reading a binary file, one std::ifstream for each thread. Now I need to know, if std::ofstream is thread-safe on Windows and Linux for writing in a same file. I am using using only one std::ofstream and using for multiple threads.
I am reading different blocks using using each thread and writing those block in output file using seekp() and write(). Currently it is working for me but whether it is problematic for big files.

std :: ofstream 线程安全吗?

推荐答案

C ++标准(输入/输出库线程安全):

From C++ standards (Input/Output Library Thread Safety):


27.1.3线程安全[iostreams.thread-safety]

27.1.3 Thread safety [iostreams.thread-safety]

并行访问流对象[string.streams,file.streams],流缓冲对象
[stream.buffers]或C库流[c .files]多个线程可能导致数据
race [intro.multithread],除非另有规定[iostream.objects]。 [注意:数据库
会导致未定义的行为[intro.multithread]。

Concurrent access to a stream object [string.streams, file.streams], stream buffer object [stream.buffers], or C Library stream [c.files] by multiple threads may result in a data race [intro.multithread] unless otherwise specified [iostream.objects]. [Note: Data races result in undefined behavior [intro.multithread].

这篇关于流线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆