Ben Hinkle wrote:
>>>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.
Doh. I'd forgotten about this particularly wonderful aspect of Phobos thread behavior.
> 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.
So perhaps this then?
foreach( Thread t; Thread.getAll() )
if( t && t != Thread.getThis() )
t.wait();
A single pass still won't handle threads that are created during the loop, but the original example didn't do that anyway.
Sean

On Tue, 8 Nov 2005 23:15:42 +0000 (UTC), 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...
I don't blame you...the documentation is very unhelpful. I've been playing with thread for an hour now and I can't get to do much useful work. Has anyone got some novice-oriented documentation on D threads?
--
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"A learning experience is one of those things that says,
'You know that thing you just did? Don't do that.'" - D.N. Adams
1/12/2005 5:35:32 PM

Derek Parnell wrote:
> On Tue, 8 Nov 2005 23:15:42 +0000 (UTC), 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...
> > I don't blame you...the documentation is very unhelpful. I've been playing
> with thread for an hour now and I can't get to do much useful work. Has
> anyone got some novice-oriented documentation on D threads?
I'll outline how threads are used in Ares and I expect 95% of it will apply to Phobos as well. There are two basic ways to handle multithreading in D, subclassing Thread or passing a function pointer to a new thread object on construction. Subclassing can be a risky proposition in C++ and other languages without garbage collection because the Thread object may be destroyed before the thread completes.
As D has GC however, this isn't an issue, and I find subclassing to be the most natural way to deal with multithreading in most cases. The function pointer method tend to be quite useful when adapting existing code or when you aren't entirely sure whether you will need to parallelize a certain function or not. Here is a short example of how to create, run, and wait for threads in D:
import std.thread;
import std.c.stdio;
class MyThread : Thread
{
// the signature of run() might be different in Phobos--
// I believe it returns an int or uint
void run()
{
printf( "Hello from MyThread\n" );
}
}
void threadFunc()
{
printf( "Hello from threadFunc\n" );
}
void main()
{
int i = 5;
void threadDelegate()
{
printf( "threadDelegate can see i = %i\n", i );
}
MyThread mt = new MyThread();
Thread tf = new Thread( &threadFunc );
Thread td = new Thread( &threadDelegate );
// threads must be started explicitly
mt.start();
tf.start();
td.start();
// you must wait for the threads to complete if you want to
// be sure their output is displayed
mt.join(); // join() is called waitFor() in Phobos
tf.join();
td.join();
}
C:\code\d>dmd test
C:\bin\dmd\bin\..\..\dm\bin\link.exe test,,,user32+kernel32/noi;
C:\code\d>test
Hello from MyThread
Hello from threadFunc
threadDelegate can see i = 5
When a thread in started, it is guaranteed to see any data changes made by the starting thread before the start() call, so threadDelegate is guaranteed to see i as 5. If you intend to subsequently change the value of i however, accesses should be done within a synchronized block.
The purpose of 'volatile' in D is basically to preserve code order when lock-free concurrency is desired, so be aware that it provides no memory visibility guarantees like 'volatile' in C++ is (mistakenly) said to. That's all I can think of offhand. If you have any more specific questions, I'd be happy to answer them.
Sean

I'll just add that subclassing Thread allows one to naturally create their own TLS (thread local storage). The alternate method, where TLS is supported via an int[] or something similar, requires some less-than-clear bit-stuffing to achieve the equivalent.
- Kris
"Sean Kelly" <sean@f4.ca> wrote in message news:dmm954$2e4t$1@digitaldaemon.com...> Derek Parnell wrote:
>> On Tue, 8 Nov 2005 23:15:42 +0000 (UTC), 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...
>>>> I don't blame you...the documentation is very unhelpful. I've been
>> playing
>> with thread for an hour now and I can't get to do much useful work. Has
>> anyone got some novice-oriented documentation on D threads?
>> I'll outline how threads are used in Ares and I expect 95% of it will apply to Phobos as well. There are two basic ways to handle multithreading in D, subclassing Thread or passing a function pointer to a new thread object on construction. Subclassing can be a risky proposition in C++ and other languages without garbage collection because the Thread object may be destroyed before the thread completes. As D has GC however, this isn't an issue, and I find subclassing to be the most natural way to deal with multithreading in most cases. The function pointer method tend to be quite useful when adapting existing code or when you aren't entirely sure whether you will need to parallelize a certain function or not. Here is a short example of how to create, run, and wait for threads in D:
>>> import std.thread;
> import std.c.stdio;
>> class MyThread : Thread
> {
> // the signature of run() might be different in Phobos--
> // I believe it returns an int or uint
> void run()
> {
> printf( "Hello from MyThread\n" );
> }
> }
>> void threadFunc()
> {
> printf( "Hello from threadFunc\n" );
> }
>> void main()
> {
> int i = 5;
>> void threadDelegate()
> {
> printf( "threadDelegate can see i = %i\n", i );
> }
>> MyThread mt = new MyThread();
> Thread tf = new Thread( &threadFunc );
> Thread td = new Thread( &threadDelegate );
>> // threads must be started explicitly
> mt.start();
> tf.start();
> td.start();
>> // you must wait for the threads to complete if you want to
> // be sure their output is displayed
> mt.join(); // join() is called waitFor() in Phobos
> tf.join();
> td.join();
> }
>> C:\code\d>dmd test
> C:\bin\dmd\bin\..\..\dm\bin\link.exe test,,,user32+kernel32/noi;
>> C:\code\d>test
> Hello from MyThread
> Hello from threadFunc
> threadDelegate can see i = 5
>>>> When a thread in started, it is guaranteed to see any data changes made by the starting thread before the start() call, so threadDelegate is guaranteed to see i as 5. If you intend to subsequently change the value of i however, accesses should be done within a synchronized block. The purpose of 'volatile' in D is basically to preserve code order when lock-free concurrency is desired, so be aware that it provides no memory visibility guarantees like 'volatile' in C++ is (mistakenly) said to. That's all I can think of offhand. If you have any more specific questions, I'd be happy to answer them.
>>> Sean

Kris wrote:
> I'll just add that subclassing Thread allows one to naturally create their own TLS (thread local storage). The alternate method, where TLS is supported via an int[] or something similar, requires some less-than-clear bit-stuffing to achieve the equivalent.
Agreed. It can be appropriate for some algorithms but I wouldn't suggest it as the default storage choice. I've also heard the Posix implementation of TLS is a tad slow, though Windows TLS should be quite decent. For what it's worth, Thread.getThis() in Ares is implemented using TLS to avoid the need to access the global thread list.
Sean

In article <137ig7kvpvpus$.1a7ft4fbi03n0$.dlg@40tude.net>, Derek Parnell says...
>>On Tue, 8 Nov 2005 23:15:42 +0000 (UTC), 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...
>>I don't blame you...the documentation is very unhelpful. I've been playing with thread for an hour now and I can't get to do much useful work. Has anyone got some novice-oriented documentation on D threads?
I haven't tried running this recently, but I got this to work in the past: http://trac.dsource.org/projects/tutorials/wiki/ThreadsExample
jcc7