www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Output to console from DerivedThread class strange

reply "Oliver Puerto" <saxo123 gmx.de> writes:
Hello,

I have a question concerning threading. I use Visual Studio with the Visual-D
plugin. The problem is somehow that when executing the code below "Derived
thread running." is displayed 3 times on the console but not before "return 0"
is reached. Then "Derived thread running." is displayed 3x all of a sudden, but
not each one by one after each other. This looks a bit strange to me.

When I unquote the Thread.sleeps after every d?.start() "Derived thread
running." is displayed immediately on the console when stepping over
Thread.sleep with the debugger. When stepping over d*.start() still nothing
happens regardless how much time I wait in the debugger till I jump to the next
line. I can't make really head or tail of this. I would expect "Derived thread
running." to appear on the console somewhen after d?.start() was executed.
Thereafter I could do an additional Thread.sleep if I wanted to. But this would
not be necessary for any "Derived thread running." message to be displayed on
the console. 

I believe there is something with the debugger I don't understand or don't
realize. Any suggestions/ideas?

Thank you, Oliver Plow


import std.stdio, core.thread;

import DerivedThread;

int main(string[] argv)
{
    DerivedThread dt1 = new DerivedThread();	
    dt1.start();	
    // Thread.sleep(dur!("seconds")( 1 ));
	
    DerivedThread dt2 = new DerivedThread();	
    dt2.start();	
    // Thread.sleep(dur!("seconds")( 1 ));
	
    DerivedThread dt3 = new DerivedThread();	
    dt3.start();	
    // Thread.sleep(dur!("seconds")( 1 ));
	
    Thread.sleep(dur!("seconds")( 4 ));    
    return 0;
}

------------------ DerivedThread.d ------------------

import std.stdio, core.thread;

class DerivedThread : Thread { 
	this() { 
		super( &run ); 
	} 

private : 
	void run() { 
		writeln("Derived thread running."); 
	}
}
-- 
Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir
belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de
Feb 07 2012
parent kraybourne <stdin kraybourne.com> writes:
I know neither Windows or D very well, but I've noticed in D on Windows, 
output seems to be held back sometimes until the app terminates.

Maybe try this:

On 2/7/12 11:28 PM, Oliver Puerto wrote:
 	void run() {
 		writeln("Derived thread running.");
stdout.flush(); // <-- added
 	}
It at least helped me in similar "weird" situations, although I'm not sure it helps you or explains anything.
Feb 09 2012