Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is fmt::print thread-safe? #1348

Closed
tankiJong opened this issue Oct 8, 2019 · 1 comment
Closed

Is fmt::print thread-safe? #1348

tankiJong opened this issue Oct 8, 2019 · 1 comment

Comments

@tankiJong
Copy link
Contributor

tankiJong commented Oct 8, 2019

Hi,

I was testing the code in multiple threads and I observed different behavior for fmt::print:
With code

      fmt::print( fmt::fg( fmt::color::blue ), "worker init\n" );

I got:
image

With code

      fmt::print( fmt::fg( fmt::color::blue ), "worker {} init\n", mId );

I got:
image

Notice that items were missing color. And the only difference in code is just two lines above.

So I am wondering is that because the print is not thread-safe? Or actually some other issues going on.

Thanks for the help :)

@vitaut
Copy link
Contributor

vitaut commented Oct 8, 2019

It is thread-safe in the sense that there are no data races but the output is not atomic meaning that text style changes and the text from different threads may interleave. It can be made atomic by placing both escape sequences that control text style and the formatted text into memory_buffer and then writing them to file in one go. PRs are welcome =).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants