Question about threads

swiftcoder swiftcoder at darkcoda.com
Tue Apr 17 08:51:55 PDT 2007


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?



More information about the Digitalmars-d-learn mailing list