Threading

Steven Schveighoffer schveiguy at yahoo.com
Mon May 11 05:49:30 PDT 2009


On Mon, 11 May 2009 08:11:22 -0400, Brok3n Halo <brokenhalo282 at gmail.com>  
wrote:

> Hi
>
> I'm new do D and am getting the hang of things for the most part, but I  
> can't
> get threading to work at all.  I couldn't find any examples specific to  
> D 1.0
> but seeing that the documentation look similar outside of the example  
> for D2,
> I followed the example at
> http://www.digitalmars.com/d/2.0/phobos/std_thread.html#Thread
>
> I tried it both was shown, but it doesn't seem to work as documented  
> there,
> probably due to changes from 1.0 to 2.0.
>
> I attached the code I derived for one of the two ways, both had the same
> result though of the method I want to be threaded just running in the  
> main
> thread when the thread object is initialized.
>
> I'm using ReBuild 0.78 (based on DMD 2.019) from Eclipse Descent on the
> Windows 7 Beta.
>
> The output of the program looks like this:
> Error: Win32 Exception
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9
> 10
> WTF!
> OH HAI
> OH HAI
> OH HAI
> OH HAI
> OH HAI
> OH HAI
> OH HAI
> OH HAI
> OH HAI
> OH HAI
>

A victim of the property syntax :)

At this line:

	testthread = new Thread(testfunc);

Try this instead:

	testthread = new Thread(&testfunc);

Note that due to the property syntax sugar of D, typing just the function  
name of a no-parameter function is the same as if you put the parentheses,  
i.e. testfunc is the same as testfunc().

So you were calling testfunc, passing the result (which is a uint) to the  
constructor for Thread (which is probably what caused the exception), and  
then running the thread.

This is why you get the count-to-10 before WTF.

Putting the address (&) symbol before the function is now passing the  
function delegate (a way to call the function) to the thread, which is  
what you want.

FWIW, I think there is an i/o bug with threading in the latest release,  
see bug 2907 http://d.puremagic.com/issues/show_bug.cgi?id=2907

-Steve


More information about the Digitalmars-d-learn mailing list