digitalmars.D.learn - Question about threads
- Ary Manzana <ary esperanto.org.ar> Apr 17 2007
- =?ISO-8859-1?Q?Jari-Matti_M=E4kel=E4?= <jmjmak utu.fi.invalid> Apr 17 2007
- Ary Manzana <ary esperanto.org.ar> Apr 17 2007
- BCS <ao pathlink.com> Apr 17 2007
- Bradley Smith <digitalmars-com baysmith.com> Apr 17 2007
- swiftcoder <swiftcoder darkcoda.com> Apr 17 2007
- BCS <ao pathlink.com> Apr 17 2007
- swiftcoder <swiftcoder darkcoda.com> Apr 17 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?
Apr 17 2007
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
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
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
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
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
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
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









BCS <ao pathlink.com> 