Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
April 17, 2007 Question about threads | ||||
---|---|---|---|---|
| ||||
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? |
April 17, 2007 Re: Question about threads | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ary Manzana | 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().
|
April 17, 2007 Re: Question about threads | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ary Manzana | 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?
|
April 17, 2007 Re: Question about threads | ||||
---|---|---|---|---|
| ||||
Posted in reply to swiftcoder | 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.
|
April 17, 2007 Re: Question about threads | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | 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. > > |
April 17, 2007 Re: Question about threads | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jari-Matti Mäkelä | 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.
|
April 17, 2007 Re: Question about threads | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ary Manzana | 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.
|
April 17, 2007 Re: Question about threads | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ary Manzana | 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();
}
---------------------------
|
Copyright © 1999-2021 by the D Language Foundation