簡體   English   中英

std::print() 線程安全嗎,是否有文本交錯問題?

[英]Is std::print() thread safe, and does it have the interleaving of text problem?

std::print()將添加到 C++23中。

我想知道的是std::print()是否是線程安全的,在沒有數據競爭的意義上

並且它是否有文本交錯問題,例如,如果我在線程 1 中有:

std::print("The quick brown fox ")
std::print("jump over the lazy dog \n")

和線程 2:

std::print("She sells ")
std::print("seashells by the seashore \n")

它能以瘋狂的順序打印嗎,像這樣:

She sells The quick brown fox seashells by the seashore \n
jump over the lazy dog \n

我想這兩個問題的答案都是肯定的,以與std::cout的行為相匹配,但是任何人都可以將我與標准所說的聯系起來嗎?

iostreamFILE* stream版本的std::print及其所有衍生版本都基於等同於使用std::format創建臨時字符串然后將該字符串推入 stream 的行為。因此,如果他們想使用多次調用 stream,他們將不得不以某種方式鎖定 stream。

無論如何,它們在內部格式方面是線程安全和原子的,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM