Weird std.stdio threading bug?

Steven Schveighoffer schveiguy at yahoo.com
Tue Apr 28 06:22:11 PDT 2009


On Tue, 28 Apr 2009 09:14:15 -0400, dsimcha <dsimcha at yahoo.com> wrote:

> == Quote from Steven Schveighoffer (schveiguy at yahoo.com)'s article
>> Shouldn't you be waiting for the threads to exit at the end of main?  I
>> wonder if the GC has been shut down by main exiting.
>> -Steve
>
> I guess you're right.  (This test program was written based on a larger  
> program
> where this effect shows up, and forgetting that was just an oversight.)   
> On the
> other hand, it still doesn't fix the problem.  Also note, in case it's  
> relevant,
> my OS is win32.
>
> import core.thread, std.stdio;
>
> void main() {
>     Thread[] myThreads;
>     foreach(i; 0..4) {
>         myThreads ~= new Thread( { doStuff(); });
>         myThreads[$ - 1].start;
>     }
>     doStuff();
> }
>
>
>
> void doStuff() {
>     while(true) {
>         synchronized {
>             writeln("Doing stuff.");
>         }
>     }
> }


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



More information about the Digitalmars-d mailing list