[Issue 2907] New: std.stdio.writeln hangs multithreaded programs.

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Apr 28 08:14:29 PDT 2009


http://d.puremagic.com/issues/show_bug.cgi?id=2907

           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
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: dsimcha at yahoo.com


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.


-- 



More information about the Digitalmars-d-bugs mailing list