Question about threads
Bradley Smith
digitalmars-com at baysmith.com
Tue Apr 17 12:59:34 PDT 2007
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();
}
---------------------------
More information about the Digitalmars-d-learn
mailing list