Bug 2907 – std.stdio.writeln hangs multithreaded programs.

Status
RESOLVED
Resolution
DUPLICATE
Severity
regression
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
x86
OS
All
Creation time
2009-04-28T10:14:00Z
Last change time
2015-06-09T01:26:27Z
Assigned to
bugzilla
Creator
dsimcha

Comments

Comment #0 by dsimcha — 2009-04-28T10:14:36Z
The following program hangs with zero percent CPU usage after a few iterations of the endless loop in the doStuff() function. When the program is changed to single threaded mode, this does not happen. Commenting out the writeln("Doing stuff."); line makes this program not hang, as it maintains CPU usage above zero indefinitely. This has been confirmed on both Windows (by me) and Linux (by Steven Schveighoffer). import core.thread, std.stdio; class Lock {} Lock lock; void main() { lock = new Lock; Thread[] myThreads; foreach(i; 0..4) { myThreads ~= new Thread( { doStuff(); }); myThreads[$ - 1].start; } doStuff(); } void doStuff() { while(true) { synchronized(lock) { writeln("Doing stuff."); } } } Things tried by Steven: Things that worked (continually output "Doing stuff."): 1. removing the synchronized(lock) statement 2. Changing the number of threads to 0 3. Changing the print line to use printf("Doing stuff.\n"); Things that still failed: 4. Changing the number of threads to 1 (which would mean 2 threads, the main thread and the sub thread). The fact that 2 worked and 3 worked indicates to me that it's not simply a bug involving the lock mechanism, it's definitely something to do with writeln.
Comment #1 by dsimcha — 2009-04-30T19:24:46Z
*** This bug has been marked as a duplicate of 2890 ***