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

Tango and thread with Stdout

Posted: 10/25/07 12:08:26 Modified: 10/25/07 13:23:04

I have made this source code and I don't know if Stdout can work with thread :

import tango.core.Thread; import tango.io.stdout; class HelloThread? : Thread

{

this() {

super( &run );

}

private :

void run() {

while(true)Stdout( "Derived thread running.\n" )();

}

}

int main(char[][] args) { // create instances of each type

HelloThread? derived_1 = new HelloThread?(); HelloThread? derived_2 = new HelloThread?(); HelloThread? derived_3 = new HelloThread?(); HelloThread? derived_4 = new HelloThread?(); HelloThread? derived_5 = new HelloThread?();

// start both threads derived_1.start();

derived_2.start(); derived_3.start(); derived_4.start(); derived_5.start(); while(true) Stdout("Thread Principal")();

return 0;

}

this code on XP with DMD and tango return this message :

Derived thread running. Derived thread running. Derived thread running. Derived thread running. Derived thread running. Derived thread running. Derived thread running. Derived thread running. Derived thread running. Derived thread running. object.Exception: Access Violation object.Exception: Access Violation

Process returned 1 (0x1) execution time : 0.110 s Press any key to continue.

This is same with one thread

Author Message

Posted: 10/25/07 16:20:39

Stdout is not expected to operate across multiple threads. There is a module tango.util.log.Trace which is equivalent to Stdout, but is thread-safe.

Stdout is not synchronized by default, because people felt that fast operation was more important than occasional multi-thread concerns. It can be reconfigured at runtime to be thread-safe where necessary.

Posted: 10/25/07 17:12:19

kris wrote:

It can be reconfigured at runtime to be thread-safe where necessary.

How is this done?

Posted: 10/25/07 18:39:05

Take a look at how Trace is configured. Basically, you inject a MutexStream? into the output chain. For Stdout, you could do that via the stream() methods exposed there, or at a lower level by doing the equivalent via Cout (Stdout uses Cout). You'd be reconfiguring the output chain in all cases - just like Trace does