Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Stdout is not thread-safe?

Moderators: kris

Posted: 09/17/08 16:54:02

I use dmd 1.035 and tango 0.99.7 to write the follwing code to try thread. It could be compiled, but when I run it, it would crash and show the following message:

tango.core.Exception.AssertException@D:\Development\DTango\bin\..\import\tango\io\Buffer.d(833): Assertion failure

I found Buffer.d and jump to line 833, which is

assert (index <= extent);

Is this a bug? Because I believe basic output function like this should be thread-safe.

Here is code I wrote:

import tango.io.Stdout;
import tango.core.Thread;

void eventHandler ()
{
    while (true) {
        Stdout ("Hello World!").newline.flush;
    }
}

void eventHandler2 ()
{
    while (true) {
        Stdout ("Goodbye World!").newline.flush;
    }
}

void main (char [][] args)
{
    Thread handler1 = new Thread (&eventHandler);
    Thread handler2 = new Thread (&eventHandler2);

    handler1.start();
    handler2.start();

  
    return;
}

-- Brian Hsu

Author Message

Posted: 09/17/08 17:00:12

After I add synchronized keyword into read function to mark it as a critical section, it works fine. But I'm not sure is this correct way to fix this problem, should I write a ticket to report this problem?

Here is what I do in Buffer.d:

        synchronized uint read (uint delegate (void[]) dg)
        {
                int count = dg (data [index..extent]);

                if (count != IConduit.Eof)
                   {
                   index += count;
                   assert (index <= extent);
                   }
                return count;
        }

-- Brian Hsu

Posted: 09/17/08 17:03:49

Hi Brian,

you are correct in Stdout not being thread-safe and it is not likely to become so. Hopefully this recent thread explains the reasoning well enough? Make Stdout thread-safe thread