View mode: basic / threaded / horizontal-split · Log in · Help
November 08, 2005
Threads and concurrency
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");
}
}
November 08, 2005
Re: Threads and concurrency
And if I can get a couple simple Thread examples working I'll post a (hopefully
useful) D Thread tutorial ...

In article <dkrbiu$17v1$1@digitaldaemon.com>, Charles says...
>
>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");
>}
>}
>
>
November 08, 2005
Re: Threads and concurrency
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
November 09, 2005
Re: Threads and concurrency
In article <dkrd8s$19km$1@digitaldaemon.com>, Sean Kelly says...

>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

Sean, thanks for the reply. I've corrected the code to allocate an mthread()
and I still get the same output. Here's my makefile,main.d and mthread.d .
I don't think I can think of a simpler example than this ...

D       = gdc
main: main.d mthread.o
$(D) -o main main.d mthread.o 
mthread.o: mthread.d
$(D) -o mthread.o -c mthread.d
c clean:
rm -f ./*.o

import mthread;
int main () {
mthread m = new mthread();
m.start();
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");
}
}
November 09, 2005
Re: Threads and concurrency
Try this:


import std.thread;
import std.c.stdio;

class mthread : Thread
{
    int run ()
    {
        foo();
        return 0;
    }

    void foo ()
    {
        printf( "inside foo\n" );
    }
}

void main()
{
    mthread m = new mthread();
    m.start();
    m.wait();
}


The problem was tha you didn't have the m.wait() line at the end of main 
so the program was ending before your thread had started.


Sean
November 09, 2005
Re: Threads and concurrency
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;
}

In article <dkre89$1alk$1@digitaldaemon.com>, Charles says...
>
>In article <dkrd8s$19km$1@digitaldaemon.com>, Sean Kelly says...
>
>>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
>
>Sean, thanks for the reply. I've corrected the code to allocate an mthread()
>and I still get the same output. Here's my makefile,main.d and mthread.d .
>I don't think I can think of a simpler example than this ...
>
>D       = gdc
>main: main.d mthread.o
>$(D) -o main main.d mthread.o 
>mthread.o: mthread.d
>$(D) -o mthread.o -c mthread.d
>c clean:
>rm -f ./*.o
>
>import mthread;
>int main () {
>mthread m = new mthread();
>m.start();
>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");
>}
>}
>
>
>
November 09, 2005
Re: Threads and concurrency
Thanks!!

In article <dkrfmi$1c1a$1@digitaldaemon.com>, Sean Kelly says...
>
>Try this:
>
>
>import std.thread;
>import std.c.stdio;
>
>class mthread : Thread
>{
>     int run ()
>     {
>         foo();
>         return 0;
>     }
>
>     void foo ()
>     {
>         printf( "inside foo\n" );
>     }
>}
>
>void main()
>{
>     mthread m = new mthread();
>     m.start();
>     m.wait();
>}
>
>
>The problem was tha you didn't have the m.wait() line at the end of main 
>so the program was ending before your thread had started.
>
>
>Sean
November 09, 2005
Re: Threads and concurrency
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.

So, while the above code probably "works", it should be corrected by 
somebody better versed (than me) in threads.


> In article <dkre89$1alk$1@digitaldaemon.com>, Charles says...
> 
>>In article <dkrd8s$19km$1@digitaldaemon.com>, Sean Kelly says...
>>
>>
>>>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
>>
>>Sean, thanks for the reply. I've corrected the code to allocate an mthread()
>>and I still get the same output. Here's my makefile,main.d and mthread.d .
>>I don't think I can think of a simpler example than this ...
>>
>>D       = gdc
>>main: main.d mthread.o
>>$(D) -o main main.d mthread.o 
>>mthread.o: mthread.d
>>$(D) -o mthread.o -c mthread.d
>>c clean:
>>rm -f ./*.o
>>
>>import mthread;
>>int main () {
>>mthread m = new mthread();
>>m.start();
>>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");
>>}
>>}
>>
>>
>>
> 
> 
>
November 09, 2005
Re: Threads and concurrency
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
November 09, 2005
Re: Threads and concurrency
>> 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.
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home