std::print() 线程安全吗?它是否存在文本交错问题?

jus*_*bie 2 c++ multithreading language-lawyer fmt c++23

std::print() 将在 C++23 中添加

我想知道是否std::print()是线程安全的,因为没有数据竞争

它是否存在文本交错问题,例如,如果我在线程 1 中存在:

std::print("The quick brown fox ")
std::print("jump over the lazy dog \n")
Run Code Online (Sandbox Code Playgroud)

和线程 2:

std::print("She sells ")
std::print("seashells by the seashore \n")
Run Code Online (Sandbox Code Playgroud)

它会以疯狂的顺序打印吗,就像这样:

She sells The quick brown fox seashells by the seashore \n
jump over the lazy dog \n
Run Code Online (Sandbox Code Playgroud)

我想这两个问题的答案都是肯定的,与 的行为相匹配std::cout,但是任何人都可以将我与标准所说的联系起来吗?

Nic*_*las 5

iostream和iostream的FILE*版本std::print及其所有衍生版本都基于与使用std::format创建临时字符串然后将该字符串推入流相同的行为。因此,如果他们想要对流进行多次调用,则必须以某种方式锁定流。

无论如何,它们的内部格式是线程安全和原子的,