std::print()
will be added in C 23.
What I'm wondering is if std::print()
is thread-safe, in the sense that there is no data race
And does it have the interleaving of text problem, for example, if I have in thread 1:
std::print("The quick brown fox ")
std::print("jump over the lazy dog \n")
and thread 2:
std::print("She sells ")
std::print("seashells by the seashore \n")
Could it print in a crazy order, like this:
She sells The quick brown fox seashells by the seashore \n
jump over the lazy dog \n
I guess the answer is yes for both questions, to be matched with the behaviour of std::cout
, but can anyone link me to what the standard says?
CodePudding user response:
Both the iostream and the FILE*
stream versions of std::print
and all of its derivatives are based on behavior equivalent to using std::format
to create a temporary string and then shoving that string into the stream. So if they want to use multiple calls to the stream, they will have to lock the stream in some way.
Regardless, they are thread safe and atomic with regard to their internal formatting,