www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Question about threads

reply Ary Manzana <ary esperanto.org.ar> writes:
Hello.

I have a simple program that creates two threads, one outputs "." to the 
console, the other "!":

----------------------------------------------
class MyThread : Thread {

	char fChar;
	this(char c) {
		fChar = c;
	}
	
	int run() {
		while(true)
			writef("%s", fChar);
		return 0;
	}
	
}

void main(char[][] args) {
	MyThread t1 = new MyThread('.');
	MyThread t2 = new MyThread('!');
	
	t1.start();
	t2.start();
}
-----------------------------------------------

Running the above program in Windows makes it crash (I get the "program 
X encountered a problem and must be closed" message). Why is this 
happenning?

I also tried having a lock object and synchronized the "writef" with 
that object with no success.

Actually, I just tried running only one of the threads and got the same 
problem. If I synchronize the write then, the program exists with no 
failure.

What am I doing wrong?
Apr 17 2007
next sibling parent reply =?ISO-8859-1?Q?Jari-Matti_M=E4kel=E4?= <jmjmak utu.fi.invalid> writes:
Ary Manzana wrote:

 class MyThread : Thread {
 
     char fChar;
     this(char c) {
         fChar = c;
     }
     
     int run() {
         while(true)
             writef("%s", fChar);
         return 0;
     }
     
 }
 
 void main(char[][] args) {
     MyThread t1 = new MyThread('.');
     MyThread t2 = new MyThread('!');
     
     t1.start();
     t2.start();
 }
 -----------------------------------------------
 
 Running the above program in Windows makes it crash (I get the "program
 X encountered a problem and must be closed" message). Why is this
 happenning?
You probably have to end the execution of t1 and t2 before returning from main().
Apr 17 2007
parent reply Ary Manzana <ary esperanto.org.ar> writes:
Jari-Matti Mäkelä escribió:
 Ary Manzana wrote:
 
 class MyThread : Thread {

     char fChar;
     this(char c) {
         fChar = c;
     }
     
     int run() {
         while(true)
             writef("%s", fChar);
         return 0;
     }
     
 }

 void main(char[][] args) {
     MyThread t1 = new MyThread('.');
     MyThread t2 = new MyThread('!');
     
     t1.start();
     t2.start();
 }
 -----------------------------------------------

 Running the above program in Windows makes it crash (I get the "program
 X encountered a problem and must be closed" message). Why is this
 happenning?
You probably have to end the execution of t1 and t2 before returning from main().
Ok. So how can I implement this? It's strange for me that starting two threads and exiting main ends the application, since there are other threads alive.
Apr 17 2007
next sibling parent BCS <ao pathlink.com> writes:
Reply to Ary,

 Ok. So how can I implement this? It's strange for me that starting two
 threads and exiting main ends the application, since there are other
 threads alive.
 
I think there is a "return when this thread does" method on Thread. I think there is also a "Get a list of all threads" call someplace.
Apr 17 2007
prev sibling parent Bradley Smith <digitalmars-com baysmith.com> writes:
Ary Manzana wrote:
 Jari-Matti Mäkelä escribió:
 Ary Manzana wrote:

 class MyThread : Thread {

     char fChar;
     this(char c) {
         fChar = c;
     }
         int run() {
         while(true)
             writef("%s", fChar);
         return 0;
     }
     }

 void main(char[][] args) {
     MyThread t1 = new MyThread('.');
     MyThread t2 = new MyThread('!');
         t1.start();
     t2.start();
 }
 -----------------------------------------------

 Running the above program in Windows makes it crash (I get the "program
 X encountered a problem and must be closed" message). Why is this
 happenning?
You probably have to end the execution of t1 and t2 before returning from main().
Ok. So how can I implement this? It's strange for me that starting two threads and exiting main ends the application, since there are other threads alive.
Maybe something like this: --------------------------- import std.thread; import std.stdio; class MyThread : Thread { bool running = true; char fChar; this(char c) { fChar = c; } int run() { while(running) writef("%s", fChar); return 0; } void stop() { running = false; } } void main(char[][] args) { MyThread t1 = new MyThread('.'); MyThread t2 = new MyThread('!'); t1.start(); t2.start(); t1.wait(5000); t1.stop(); t2.stop(); } ---------------------------
Apr 17 2007
prev sibling parent reply swiftcoder <swiftcoder darkcoda.com> writes:
It looks as if your format specifier is wrong, %s is used to print strings, not
individual chars. Use %c for that.

Ary Manzana Wrote:

 Hello.
 
 I have a simple program that creates two threads, one outputs "." to the 
 console, the other "!":
 
 ----------------------------------------------
 class MyThread : Thread {
 
 	char fChar;
 	this(char c) {
 		fChar = c;
 	}
 	
 	int run() {
 		while(true)
 			writef("%s", fChar);
 		return 0;
 	}
 	
 }
 
 void main(char[][] args) {
 	MyThread t1 = new MyThread('.');
 	MyThread t2 = new MyThread('!');
 	
 	t1.start();
 	t2.start();
 }
 -----------------------------------------------
 
 Running the above program in Windows makes it crash (I get the "program 
 X encountered a problem and must be closed" message). Why is this 
 happenning?
 
 I also tried having a lock object and synchronized the "writef" with 
 that object with no success.
 
 Actually, I just tried running only one of the threads and got the same 
 problem. If I synchronize the write then, the program exists with no 
 failure.
 
 What am I doing wrong?
Apr 17 2007
parent reply BCS <ao pathlink.com> writes:
Reply to SwiftCoder,

 It looks as if your format specifier is wrong, %s is used to print
 strings, not individual chars. Use %c for that.
 
 Ary Manzana Wrote:
 
IIRC %s is the "whatever" format. It will print anything.
Apr 17 2007
parent swiftcoder <swiftcoder darkcoda.com> writes:
Ah, yes you are right, I had forgotten that. But according to the docs it
should print chars as if they were integers (probably an oversight in the docs
though): http://www.digitalmars.com/d/phobos/std_format.html#format-string They
also don't mention any char-specific format specifier.

BCS Wrote:

 Reply to SwiftCoder,
 
 It looks as if your format specifier is wrong, %s is used to print
 strings, not individual chars. Use %c for that.
 
 Ary Manzana Wrote:
 
IIRC %s is the "whatever" format. It will print anything.
Apr 17 2007