www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Threads

reply DF <deefriend ymail.com> writes:
/**
 * Testing.
 */
module Test;

import std.thread;
import std.stdio;

class DerivedThread : Thread {

     this() {
         super(&run);
     }

private :
     int run() {
        writefln("Derived thread running.\n" );
	return 0;
     }
}

void main() {
	Thread derived = new DerivedThread();
	derived.start();
}

This code makes no output. Why?
Nov 19 2008
next sibling parent reply Spacen Jasset <spacenjasset yahoo.co.uk> writes:
DF wrote:
 /**
  * Testing.
  */
 module Test;
 
 import std.thread;
 import std.stdio;
 
 class DerivedThread : Thread {
 
      this() {
          super(&run);
      }
 
 private :
      int run() {
         writefln("Derived thread running.\n" );
 	return 0;
      }
 }
 
 void main() {
 	Thread derived = new DerivedThread();
 	derived.start();
 }
 
 This code makes no output. Why?

Not quite sure, but you could try derived.wait() in main to wait for the thread to finish.
Nov 19 2008
next sibling parent reply DF <deefriend ymail.com> writes:
Spacen Jasset Wrote:

 DF wrote:
 /**
  * Testing.
  */
 module Test;
 
 import std.thread;
 import std.stdio;
 
 class DerivedThread : Thread {
 
      this() {
          super(&run);
      }
 
 private :
      int run() {
         writefln("Derived thread running.\n" );
 	return 0;
      }
 }
 
 void main() {
 	Thread derived = new DerivedThread();
 	derived.start();
 }
 
 This code makes no output. Why?

Not quite sure, but you could try derived.wait() in main to wait for the thread to finish.

Nov 19 2008
parent reply Sean Kelly <sean invisibleduck.org> writes:
DF wrote:
 Spacen Jasset Wrote:
 
 DF wrote:
 /**
  * Testing.
  */
 module Test;

 import std.thread;
 import std.stdio;

 class DerivedThread : Thread {

      this() {
          super(&run);
      }

 private :
      int run() {
         writefln("Derived thread running.\n" );
 	return 0;
      }
 }

 void main() {
 	Thread derived = new DerivedThread();
 	derived.start();
 }

 This code makes no output. Why?

thread to finish.


If you're using D 2.0 then that shouldn't even compile. In fact, std.thread doesn't exist any longer. Try: module Test; import core.thread; import std.stdio; class DerivedThread : Thread { this() { super(&run); } private: void run() { writefln( "Derived thread running." ); } } void main() { Thread derived = new DerivedThread(); derived.start(); } Note that if you set "Thread.isDaemon = true" then you will need to join() the thread to wait for it to complete before the app exits. Sean
Nov 19 2008
parent DF <deefriend ymail.com> writes:
Sean Kelly Wrote:

 DF wrote:
 Spacen Jasset Wrote:
 
 DF wrote:
 /**
  * Testing.
  */
 module Test;

 import std.thread;
 import std.stdio;

 class DerivedThread : Thread {

      this() {
          super(&run);
      }

 private :
      int run() {
         writefln("Derived thread running.\n" );
 	return 0;
      }
 }

 void main() {
 	Thread derived = new DerivedThread();
 	derived.start();
 }

 This code makes no output. Why?

thread to finish.


If you're using D 2.0 then that shouldn't even compile. In fact, std.thread doesn't exist any longer. Try: module Test; import core.thread; import std.stdio; class DerivedThread : Thread { this() { super(&run); } private: void run() { writefln( "Derived thread running." ); } } void main() { Thread derived = new DerivedThread(); derived.start(); } Note that if you set "Thread.isDaemon = true" then you will need to join() the thread to wait for it to complete before the app exits. Sean

That's what I get when I try to compile the code you've posted. module thread cannot read file 'core\thread.d'
Nov 19 2008
prev sibling parent "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
On Wed, Nov 19, 2008 at 2:53 PM, DF <deefriend ymail.com> wrote:
 Sean Kelly Wrote:

 DF wrote:
 Spacen Jasset Wrote:

 DF wrote:
 /**
  * Testing.
  */
 module Test;

 import std.thread;
 import std.stdio;

 class DerivedThread : Thread {

      this() {
          super(&run);
      }

 private :
      int run() {
         writefln("Derived thread running.\n" );
   return 0;
      }
 }

 void main() {
   Thread derived = new DerivedThread();
   derived.start();
 }

 This code makes no output. Why?

thread to finish.


If you're using D 2.0 then that shouldn't even compile. In fact, std.thread doesn't exist any longer. Try: module Test; import core.thread; import std.stdio; class DerivedThread : Thread { this() { super(&run); } private: void run() { writefln( "Derived thread running." ); } } void main() { Thread derived = new DerivedThread(); derived.start(); } Note that if you set "Thread.isDaemon = true" then you will need to join() the thread to wait for it to complete before the app exits. Sean

That's what I get when I try to compile the code you've posted. module thread cannot read file 'core\thread.d'

You're probably using an older compiler. std.thread has recently become core.thread. Just change it to std.thread, or update your compiler.
Nov 19 2008
prev sibling next sibling parent reply "Robert Jacques" <sandford jhu.edu> writes:
On Wed, 19 Nov 2008 13:26:32 -0500, DF <deefriend ymail.com> wrote:

 void main() {
 	Thread derived = new DerivedThread();
 	derived.start();
 }

The main thread is exiting before the derived thread runs. This works correctly: Thread derived = new DerivedThread(); derived.start(); derived.wait;
Nov 19 2008
parent DF <deefriend ymail.com> writes:
Robert Jacques Wrote:

 On Wed, 19 Nov 2008 13:26:32 -0500, DF <deefriend ymail.com> wrote:
 
 void main() {
 	Thread derived = new DerivedThread();
 	derived.start();
 }

The main thread is exiting before the derived thread runs. This works correctly: Thread derived = new DerivedThread(); derived.start(); derived.wait;

You'll get "Error: hidden method called for Test.DerivedThread" if try to use derived.wait.
Nov 19 2008
prev sibling next sibling parent reply DF <deefriend ymail.com> writes:
DF Wrote:

 /**
  * Testing.
  */
 module Test;
 
 import std.thread;
 import std.stdio;
 
 class DerivedThread : Thread {
 
      this() {
          super(&run);
      }
 
 private :
      int run() {
         writefln("Derived thread running.\n" );
 	return 0;
      }
 }
 
 void main() {
 	Thread derived = new DerivedThread();
 	derived.start();
 }
 
 This code makes no output. Why?

The code above should be changed to: /** * Testing. */ module Test; import std.thread; import std.stdio; class DerivedThread : Thread { this() { super(&run); } int run() { writefln("Derived thread running.\n" ); return 0; } } void main() { Thread derived = new DerivedThread(); derived.start(); derived.wait(); } Now it works thanks.
Nov 19 2008
parent DF <deefriend ymail.com> writes:
DF Wrote:

 DF Wrote:
 
 /**
  * Testing.
  */
 module Test;
 
 import std.thread;
 import std.stdio;
 
 class DerivedThread : Thread {
 
      this() {
          super(&run);
      }
 
 private :
      int run() {
         writefln("Derived thread running.\n" );
 	return 0;
      }
 }
 
 void main() {
 	Thread derived = new DerivedThread();
 	derived.start();
 }
 
 This code makes no output. Why?

The code above should be changed to: /** * Testing. */ module Test; import std.thread; import std.stdio; class DerivedThread : Thread { this() { super(&run); } int run() { writefln("Derived thread running.\n" ); return 0; } } void main() { Thread derived = new DerivedThread(); derived.start(); derived.wait(); } Now it works thanks.

P.S. I'm using Digital Mars D Compiler v2.014
Nov 19 2008
prev sibling parent "Robert Jacques" <sandford jhu.edu> writes:
On Wed, 19 Nov 2008 13:33:26 -0500, DF <deefriend ymail.com> wrote:

 Robert Jacques Wrote:

 On Wed, 19 Nov 2008 13:26:32 -0500, DF <deefriend ymail.com> wrote:

 void main() {
 	Thread derived = new DerivedThread();
 	derived.start();
 }

The main thread is exiting before the derived thread runs. This works correctly: Thread derived = new DerivedThread(); derived.start(); derived.wait;

You'll get "Error: hidden method called for Test.DerivedThread" if try to use derived.wait.

This worked on my setup (D 1.035 + Phobos + Windows + Release), which version are you using?
Nov 19 2008