April 05, 2005 Re: Threads not independent... | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Larry Cowan | Try this:
import std.c.windows.windows;
import std.thread;
import std.stdio;
class RThread : Thread
{
int index;
this(int i)
{
super(&run);
index = i;
}
int run()
{
writefln("thread %d",index);
//allDone.countDown();
return 0;
}
}
int main(char[][] args)
{
//CountDownLatch allDone = new CountDownLatch(5);
RThread[5] threads;
for (int i = 0; i < threads.length; i++)
threads[i] = new RThread(i);
foreach(RThread t; threads)
t.start();
//allDone.wait();
Sleep(10000);
printf("All done.\n");
return 0;
}
Note: I dont have the concurrent stuff so I have included it but commented it out.
Regan
| |||
April 05, 2005 Re: Threads not independent... | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Larry Cowan | Alternate solution using seperate object, not derived from Thread:
import std.c.windows.windows;
import std.thread;
import std.stdio;
class Other
{
int index;
this(int i)
{
index = i;
}
int run()
{
writefln("thread %d",index);
//allDone.countDown();
return 0;
}
}
int main(char[][] args)
{
//CountDownLatch allDone = new CountDownLatch(5);
Thread[5] threads;
Other o;
for (int i = 0; i < threads.length; i++) {
o = new Other(i);
threads[i] = new Thread(&o.run);
}
foreach(Thread t; threads)
t.start();
//allDone.wait();
Sleep(10000);
printf("All done.\n");
return 0;
}
Regan
| |||
April 05, 2005 Re: Threads not independent... | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Larry Cowan | Ooops. One important correction is that the 'allDone' variable actually needs to be global, or otherwise accessable from the RThread or Other classes. eg.
import ..etc..
//global variable/singleton
CountDownLatch allDone;
//module static ctor, executed when program starts
static this()
{
allDone = new CountDownLatch(5);
}
class RThread : Thread
{
..etc..
}
class Other
{
..etc..
}
int main(char[][] args)
{
..etc..
}
Regan
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply