Question about threads

Ary Manzana ary at esperanto.org.ar
Tue Apr 17 05:59:21 PDT 2007


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