cout 是同步的/线程安全的吗? [英] Is cout synchronized/thread-safe?

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

问题描述

一般来说,我假设流是不同步的,这取决于用户进行适当的锁定.但是,像 cout 这样的东西在标准库中得到特殊处理了吗?

In general I assume that streams are not synchronized, it is up to the user to do appropriate locking. However, do things like cout get special treatment in the standard library?

也就是说,如果多个线程正在写入 cout 是否会破坏 cout 对象?我知道即使同步你仍然会得到随机交错的输出,但交错是否有保证.也就是说,从多个线程使用 cout 是否安全?

That is, if multiple threads are writing to cout can they corrupt the cout object? I understand that even if synchronized you'd still get randomly interleaved output, but is that interleaving guaranteed. That is, is it safe to use cout from multiple threads?

该供应商是否依赖?gcc 有什么作用?

Is this vendor dependent? What does gcc do?

重要:如果您说是",请为您的回答提供某种参考,因为我需要某种证明.

Important: Please provide some kind of reference for your answer if you say "yes" since I need some kind of proof of this.

我关心的也不在于底层系统调用,这些都很好,但流在顶部添加了一层缓冲.

My concern is also not about the underlying system calls, those are fine, but the streams add a layer of buffering on top.

推荐答案

C++03 标准对此没有任何说明.当您无法保证某事物的线程安全性时,您应该将其视为非线程安全的.

The C++03 standard does not say anything about it. When you have no guarantees about the thread-safety of something, you should treat it as not thread-safe.

这里特别有趣的是 cout 被缓冲的事实.即使对 write 的调用(或在该特定实现中实现该效果的任何东西)保证是互斥的,缓冲区也可能由不同的线程共享.这将很快导致流内部状态的损坏.

Of particular interest here is the fact that cout is buffered. Even if the calls to write (or whatever it is that accomplishes that effect in that particular implementation) are guaranteed to be mutually exclusive, the buffer might be shared by the different threads. This will quickly lead to corruption of the internal state of the stream.

即使保证对缓冲区的访问是线程安全的,您认为这段代码会发生什么?

And even if access to the buffer is guaranteed to be thread-safe, what do you think will happen in this code?

// in one thread
cout << "The operation took " << result << " seconds.";

// in another thread
cout << "Hello world! Hello " << name << "!";

您可能希望此处的每一行都相互排斥.但实施如何保证这一点?

You probably want each line here to act in mutual exclusion. But how can an implementation guarantee that?

在 C++11 中,我们确实有一些保证.FDIS 在 §27.4.1 [iostream.objects.overview] 中说明以下内容:

In C++11, we do have some guarantees. The FDIS says the following in §27.4.1 [iostream.objects.overview]:

不应导致多线程同时访问同步(第 27.5.3.4 节)标准 iostream 对象的格式化和未格式化输入(第 27.7.2.1 节)和输出(第 27.7.3.1 节)函数或标准 C 流在数据竞赛中(第 1.10 节).[注意:用户仍然必须同步这些对象和流的并发使用多线程,如果他们希望避免交错字符.— 尾注 ]

Concurrent access to a synchronized (§27.5.3.4) standard iostream object’s formatted and unformatted input (§27.7.2.1) and output (§27.7.3.1) functions or a standard C stream by multiple threads shall not result in a data race (§1.10). [ Note: Users must still synchronize concurrent use of these objects and streams by multiple threads if they wish to avoid interleaved characters. — end note ]

因此,您不会得到损坏的流,但如果您不希望输出成为垃圾,您仍然需要手动同步它们.

So, you won't get corrupted streams, but you still need to synchronize them manually if you don't want the output to be garbage.

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

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