Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is putStrLn thread-safe?

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?

like image 737
ErikR Avatar asked Nov 25 '12 09:11

ErikR


People also ask

Is synchronized and thread safe same?

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.

Is crypt thread safe?

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.


1 Answers

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.

like image 56
shachaf Avatar answered Oct 03 '22 12:10

shachaf