www.digitalmars.com         C & C++   DMDScript  

D - threads

reply "Carlos Santander B." <carlos8294 msn.com> writes:
I have something like this:

class MyThread : Thread { ... }
...
MyThread a,b,c;
...

I want to run a,b and c, all at the same time. How can I do it? If I use
a.run(), etc., they will execute, wait until finishing and then go to the
next instruction. If I use a.start(), etc., they will execute, continue to
the next instruction, but the program will exit without waiting for them to
finish. If after a.start()... I use a.wait()..., they will end in the order
I specify. So let's suppose I'm simulating a Formula 1 race, and I specify
the cars speed by the priority of the thread. There's no way (for me) to
simulate it properly because of the given reasons. Am I missing something?

-------------------------
Carlos Santander


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.491 / Virus Database: 290 - Release Date: 2003-06-18
Jun 23 2003
next sibling parent "Vathix" <Vathix dprogramming.com> writes:
I think your problem is that the current thread is returning from main
before the other threads finish executing, causing the program to end. So,
you'd have to make the current thread wait for the others like
Thread.getThis().wait();  but you'll have to resume this thread some time or
the program will stay frozen, so maybe you can synchronize a variable that
indicates how many threads (out of a b c) have finished, and the last one
resume the main thread.


"Carlos Santander B." <carlos8294 msn.com> wrote in message
news:bd7jrl$1fog$1 digitaldaemon.com...
 I have something like this:

 class MyThread : Thread { ... }
 ...
 MyThread a,b,c;
 ...

 I want to run a,b and c, all at the same time. How can I do it? If I use
 a.run(), etc., they will execute, wait until finishing and then go to the
 next instruction. If I use a.start(), etc., they will execute, continue to
 the next instruction, but the program will exit without waiting for them
to
 finish. If after a.start()... I use a.wait()..., they will end in the
order
 I specify. So let's suppose I'm simulating a Formula 1 race, and I specify
 the cars speed by the priority of the thread. There's no way (for me) to
 simulate it properly because of the given reasons. Am I missing something?

 -------------------------
 Carlos Santander


 ---
 Outgoing mail is certified Virus Free.
 Checked by AVG anti-virus system (http://www.grisoft.com).
 Version: 6.0.491 / Virus Database: 290 - Release Date: 2003-06-18
Jun 23 2003
prev sibling parent Helmut Leitner <helmut.leitner chello.at> writes:
"Carlos Santander B." wrote:
 
 I have something like this:
 
 class MyThread : Thread { ... }
 ...
 MyThread a,b,c;
 ...
 
 I want to run a,b and c, all at the same time. How can I do it? If I use
 a.run(), etc., they will execute, wait until finishing and then go to the
 next instruction. If I use a.start(), etc., they will execute, continue to
 the next instruction, but the program will exit without waiting for them to
 finish. If after a.start()... I use a.wait()..., they will end in the order
 I specify. So let's suppose I'm simulating a Formula 1 race, and I specify
 the cars speed by the priority of the thread. There's no way (for me) to
 simulate it properly because of the given reasons. Am I missing something?
My first try used Thread.nthreads: /* thread1.d (C) Helmut Leitner 2003, PD */ import thread; Thread t1; Thread t2; int i; int print1(void *p) { int n=10; while(n--) { printf("T1.n=%d (%d)\n",n,i); Sleep(20); } return 0; } int print2(void *p) { int n=20; while(n--) { printf("T2.n=%d (%d)\n",n,i); Sleep(10); } return 0; } int main (char[][] args) { printf("START\n"); t1=new Thread(&print1,null); t2=new Thread(&print2,null); t1.start(); t2.start(); do { for(i=1; i<1000000000; i++) {} printf("w"); Sleep(1); } while(Thread.nthreads>1) printf("END\n"); return 0; } -- Helmut Leitner leitner hls.via.at Graz, Austria www.hls-software.com
Jun 23 2003