digitalmars.D.learn - Question about threads
- Ary Manzana (34/34) Apr 17 2007 Hello.
- =?ISO-8859-1?Q?Jari-Matti_M=E4kel=E4?= (3/30) Apr 17 2007 You probably have to end the execution of t1 and t2 before returning
- Ary Manzana (4/36) Apr 17 2007 Ok. So how can I implement this? It's strange for me that starting two
- BCS (3/7) Apr 17 2007 I think there is a "return when this thread does" method on Thread. I th...
- Bradley Smith (30/64) Apr 17 2007 Maybe something like this:
- swiftcoder (2/44) Apr 17 2007
- BCS (2/7) Apr 17 2007 IIRC %s is the "whatever" format. It will print anything.
- swiftcoder (2/13) 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: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.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
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ó: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(); } ---------------------------Ary Manzana wrote: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.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
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