Summary: std.stdio.writeln hangs multithreaded programs.
           Product: D
           Version: 2.029
          Platform: PC
        OS/Version: All
            Status: NEW
          Severity: regression
          Priority: P2
         Component: Phobos

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;

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


Reply via email to