www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2907] New: std.stdio.writeln hangs multithreaded programs.

reply d-bugmail puremagic.com writes:

           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 digitalmars.com
        ReportedBy: dsimcha 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;

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

Apr 28 2009
parent d-bugmail puremagic.com writes:

dsimcha yahoo.com changed:

           What    |Removed                     |Added
             Status|NEW                         |RESOLVED
         Resolution|                            |DUPLICATE

------- Comment #1 from dsimcha yahoo.com  2009-04-30 19:24 -------

*** This bug has been marked as a duplicate of 2890 ***

Apr 30 2009