Weird std.stdio threading bug?

dsimcha dsimcha at yahoo.com
Tue Apr 28 06:51:54 PDT 2009


== Quote from Steven Schveighoffer (schveiguy at yahoo.com)'s article
> Have you tried synchronizing on an actual object?  I remember some time
> back how Walter proposed removing synchronized as you have written it.
> Not sure what happened for that.
> The way you have written the code, assuming that the synchronized
> statement is doing what you think it's doing, the call to writeln should
> be completely syncrhonous, so multithreading issues or not, it should work.
> -Steve

Good idea, still doesn't work.  All of the loops just die after a few iterations,
leaving me at 0% CPU usage.  This happens on multiple win32 boxes.  Could someone
please test this on some other OS?

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.");
        }
    }
}



More information about the Digitalmars-d mailing list