I know that if I have multiple threads calling putStrLn
without any kind of concurrency control that the output of the threads may be interleaved.
My question is whether putStrLn
is thread-safe modulo this interleaved output?
I am presuming that putStrLn
is a buffered write operation, so I'm really asking if any corruption of the output buffer can occur by having two threads call putStrLn
at the same time.
And in general, what can be said about the thread safety of Haskell's (really GHC's) other "standard I/O" functions? In particular, for any of the buffered read operations is it possible for the same character to get returned to two different threads making the same read call at the same time?
Thread safe means that a method or class instance can be used by multiple threads at the same time without any problems occurring. Where as Synchronized means only one thread can operate at single time.
no, it's not threadsafe. Since it's using the SAME internal static data structure, two threads calling crypt() at the same time will start trashing each other's data.
Yes, it's thread-safe in the sense that you're asking about. A Handle
is protected by an MVar
which won't allow the buffer to become corrupted. As you pointed out, though, interleaving is a different matter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With