Community

While on the subject of Threads would someone please hit me w/ the clue bat b/c
I can't seem to get the following working. This example should just call
mthread.run since I've overloaded it . But that's not what happens. It just call
the mthread constructor and finishes. I'd also appreciate any links to some
beginner Thread examples in D. I'm sure the locks code would come in handy at
some point but I'm not there yet.
int main () {
mthread m = new Thread();
m.run(); // <== this should call mthread.run but it just calls the constructor
return 0;
}
module mthread;
import std.thread;
class mthread : Thread {
this () {
super();
std.string.writefln("S thread cons");
std.string.writefln("E thread cons");
}
~this() { }
int run () { foo(); }
void foo () {
std.string.writefln("inside foo");
}
void bar () {
std.string.writefln("inside bar");
}
}

Charles wrote:
> While on the subject of Threads would someone please hit me w/ the clue bat b/c
> I can't seem to get the following working. This example should just call
> mthread.run since I've overloaded it . But that's not what happens. It just call
>
> the mthread constructor and finishes.
...
> int main () {
> mthread m = new Thread();
I'm surprised this is even allowed. You're constructing an instance of
the base class, not mthread. Try this:
mthread m = new mthread();
Sean

Georg Wrede wrote:
> Charles wrote:
>
>> okay, I solved my own problem ... I forgot to call wait() ... christ.
>> Here my new main.d
>>
>> import mthread;
>> int main () {
>> mthread m;
>> for( int i;i<10;i++) {
>> m = new mthread(i);
>> m.start();
>> }
>> m.wait(); return 0;
>> }
>
>
> I hope the above code was just "a sketch" thrown in, since I don't think
> it is quite right?
>
> ((Somebody correct me if I'm wrong here: ))
>
> In the for loop:
> - you create a thread
> - you start the thread
> - you abandon the thread by assigning a new one to m
> - thus you can't reach any of the previously created threads
>
> Luckily, ;-( the previous threads don't get reaped by the GC, because
> std.thread itself has to keep pointers to them. (And you can always
> issue Thread.getAll() to get all the running threads, but it's not a
> substitute for properly being able to e.g. pause, wait, or resume a
> specific known thread.)
>
> After the loop, main waits only for the last created thread. Now, in
> real code this would be dangerous because there's no guarantee that it
> finishes last.
Yup. The code should probably do something like this instead:
foreach( Thread t; Thread.getAll() )
t.wait();
Sean

>> After the loop, main waits only for the last created thread. Now, in real
>> code this would be dangerous because there's no guarantee that it
>> finishes last.
>
> Yup. The code should probably do something like this instead:
>
> foreach( Thread t; Thread.getAll() )
> t.wait();
where "something like" doesn't actually call Thread.getAll(), right?
Glancing over std.thread getAll returns a slice of the static thread array
so its contents may change as the foreach loops over it - ie some elements
might be null if that thread happened to stop by the time the foreach got to
it. Also Thread.wait errors when you try to wait on yourself so since getAll
includes the current thread that loop will error at some point.