Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 11, 2009 Threading | ||||
---|---|---|---|---|
| ||||
Attachments: | 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 Any help would be awesome, thanks. |
May 11, 2009 Re: Threading | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brok3n Halo | On Mon, 11 May 2009 08:11:22 -0400, Brok3n Halo <brokenhalo282@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 |
May 11, 2009 Re: Threading | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | Thanks for the quick reply but I had the & to begain with and I was getting an error. I'm not at my computer right now so unfortunetly I can't see what it was, but I think it was something along the lines of thread expecting a function but that's a delegate. I'll try and find the time to set up the project on my laptop, and see what the error was for sure in the next few hours. |
May 11, 2009 Re: Threading | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brok3n Halo | On Mon, 11 May 2009 11:45:34 -0400, Brok3n Halo <X2ndshadow@hotmail.com> wrote:
> Thanks for the quick reply but I had the & to begain with and I was getting an error. I'm not at my computer right
> now so unfortunetly I can't see what it was, but I think it was something along the lines of thread expecting a
> function but that's a delegate. I'll try and find the time to set up the project on my laptop, and see what the error
> was for sure in the next few hours.
ah, different issue.
Your thread function needs to return void. Thread should accept either a function pointer or a delegate.
So it should look like:
void testfunc()
And kill the return 0.
-Steve
|
May 11, 2009 Re: Threading | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | ah-ha! I was just writing to let you know that's how my project started out and I went into this issue, when I decided to try it again and got an error message I know how to solve. Probably got it before too, but working off an all-nighter and my caffeine hasn't fully kicked in yet. Turns out it's expecting an int testfunc(), the reason it wasn't working before was I was trying to use a uint testfunc() Thanks for the heads up on the I/O issue with threading, the writefln wasn't producing any output once I got it going and thanks to that I know what the issue with that was right away, luckily my actual project doesn't require I/O on a thread. (I'm writing a multi-threaded raytracer for a class) Which brings me to my next 2 questions, are dynamic arrays thread safe in D? I imagine they should be as long as I don't have two threads writing to the same index at once and the array size doesn't change. Also is there a thread safe linked list somewhere? |
May 11, 2009 Re: Threading | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brok3n Halo | On Mon, 11 May 2009 13:46:39 -0400, Brok3n Halo <x2ndshadow@hotmail.com> wrote: > ah-ha! I was just writing to let you know that's how my project started out and I > went into this issue, when I decided to try it again and got an error message I > know how to solve. Probably got it before too, but working off an all-nighter and > my caffeine hasn't fully kicked in yet. Turns out it's expecting an int > testfunc(), the reason it wasn't working before was I was trying to use a uint > testfunc() > > Thanks for the heads up on the I/O issue with threading, the writefln wasn't > producing any output once I got it going and thanks to that I know what the issue > with that was right away, luckily my actual project doesn't require I/O on a > thread. (I'm writing a multi-threaded raytracer for a class) > > Which brings me to my next 2 questions, are dynamic arrays thread safe in D? I > imagine they should be as long as I don't have two threads writing to the same > index at once and the array size doesn't change. They are no more thread safe than any other construct. There is no special care taken to ensure writes or reads are thread safe, it is up to you to ensure that. > Also is there a thread safe > linked list somewhere? As far as I know, there are no builtin linked lists in D's standard library. Tango, an alternate standard lib for D1, has linked lists, and dcollections, a collection package I wrote for both D1 phobos and Tango, has linked lists, neither of which has thread safety built in. -Steve |
May 12, 2009 Re: Threading | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer Attachments: | I probably should have phrased my question better with the array, what I was wondering is if it was safe for say two threads to right to the array at the same time as long as I'm sure they're not writing to the same index of the array? Also I'm still getting the "Error: Win32 Exception" with my test code any idea why? Attached is the latest version. |
May 12, 2009 Re: Threading | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brok3n Halo | I have never used threads before, but it sounds safe to me as when the thread gets thrown of the core and later gets back nothing referred by the register has changed. Again, I am a newbie :) "Brok3n Halo" <x2ndshadow@hotmail.com> wrote in message news:gubegl$uup$1@digitalmars.com... >I probably should have phrased my question better with the array, what I was > wondering is if it was safe for say two threads to right to the array at > the same > time as long as I'm sure they're not writing to the same index of the > array? > > Also I'm still getting the "Error: Win32 Exception" with my test code any > idea > why? Attached is the latest version. > |
May 12, 2009 Re: Threading | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brok3n Halo | On Tue, 12 May 2009 05:11:49 -0400, Brok3n Halo <x2ndshadow@hotmail.com> wrote: > I probably should have phrased my question better with the array, what I was > wondering is if it was safe for say two threads to right to the array at the same > time as long as I'm sure they're not writing to the same index of the array? You can do anything you want without thread safety, but you run the risk of deadlocks or corrupted memory. The problem isn't writing to two different elements of an array, the hard part is *ensuring* that you are writing to two different elements of an array. Multithreading code is tough to write correctly, you may want to read a book on it. D2 promises to be a lot better at helping you ensure this. > > Also I'm still getting the "Error: Win32 Exception" with my test code any idea > why? Attached is the latest version. Just realized from reading your code, you are using D1 with Phobos, I have no idea what bugs there are, or how to use threads there, so I can't really help you. I can tell you that your code ported to Tango runs without throwing an exception, code below: import tango.core.Thread; import tango.io.Stdout; int count = 0; void main(){ Thread testthread; void testfunc(){ while(count<10000000){ ++count; } } testthread = new Thread(&testfunc); testthread.start(); int lastCount = 0; while(testthread.isRunning){ if(count != lastCount){ Stdout.formatln("{}", count); lastCount = count; } } } output: 164 16789 23750 29998 36054 4472263 4482283 4488871 4495320 4501356 4507264 4513158 4518987 4524886 4530722 4536557 4542362 4548221 4554051 4559848 4565753 4571592 4577354 4583152 4588942 4594719 4600579 4606375 4612181 4617981 4623686 4629421 -Steve |
May 12, 2009 Re: Threading | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | > >> I probably should have phrased my question better with the array, what I >> was >> wondering is if it was safe for say two threads to right to the array at >> the same >> time as long as I'm sure they're not writing to the same index of the >> array? > > You can do anything you want without thread safety, but you run the risk of deadlocks or corrupted memory. That is why the question was whether it was safe. > > The problem isn't writing to two different elements of an array, >the hard part is *ensuring* that you are writing to two different elements of an array. Multithreading code is tough to write correctly, you may want to read a book on it. And sometimes it is extremely easy to ensure you are never writing to the same elements. |
Copyright © 1999-2021 by the D Language Foundation