Closed your-diary closed 7 months ago
What exactly do you mean by "thread safe"?
It won't print hewollrlod
, but it will either print "hello\nworld\n" or "world\nhello\n"!
@matthiasbeyer
What exactly do you mean by "thread safe"?
I wanted to mean "can this library print hewollrlod
"?
It won't print
hewollrlod
Is this behavior guaranteed? If so, to confirm it, do I have to read the source code? Or is it written in some documentation?
Maybe its a bias of mine but that would be expected within the Rust ecosystem. Just like println!
will not interleave text (but multiple println
s will), a logged event should not be interleaved.
@epage
As far as I understand, println!()
is thread-safe because it always locks and unlocks /dev/stdout
(unofficial source).
So I think it is possible this library avoids println!()
family to improve performance because locking / unlocking is relatively slow. Such an implementation is clearly problematic in using multi-threaded context, but safe in single-threaded context.
std
only exposes stdout
/ stderr
with locking. This is independent of the println
macro.. You can explicitly acquire the lock to be able to make multiple calls, but there is always a lock. You'd need to pull in something like libc
to do output without a lock which is enough off the beaten path that it isn't a general assumption when working within Rust code.
The bigger risk is making multiple calls to stdout
/ stderr
without grabbing the lock. This is both slower (multiple aquire/releases) and can lead to interleaved output.
And to get back to my point, the general expectation within the Rust community is that println
and println
-like macros, like the log
macros, will not interleave.
Is this library thread-safe?
README.md
and the official documentation say nothing about that.Is this code safe?