www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Problem about multi-thread programming

reply "Tongzhou Li" <zhangsongcui hotmail.com> writes:
Hello everyone! I'm learning D and trying to write some sample 
code in D.
I wrote: http://codepad.org/K4xQOREZ
It compiles well with dmd 2.0.59, but I got an error when running:
     object.Error: Access Violation
Any one help? Thanks.
May 05 2012
next sibling parent "Kagamin" <spam here.lot> writes:
What's in console?
May 05 2012
prev sibling next sibling parent "Tongzhou Li" <zhangsongcui hotmail.com> writes:
On Saturday, 5 May 2012 at 09:16:04 UTC, Kagamin wrote:
 What's in console?

object.Error: Access Violation ---------------- ---------------- Expected output: 12341234123.... PS: Line 13 should be: write(Idx);
May 05 2012
prev sibling next sibling parent "Chris Cain" <clcain uncg.edu> writes:
On Saturday, 5 May 2012 at 09:28:34 UTC, Tongzhou Li wrote:
 On Saturday, 5 May 2012 at 09:16:04 UTC, Kagamin wrote:
 What's in console?

object.Error: Access Violation ---------------- ---------------- Expected output: 12341234123.... PS: Line 13 should be: write(Idx);

By default, everything is TLS (thread local) ... so, cv[0] will be null in main even though another thread set it to something else. Adding __gshared in front of "Condition[4] cv;" on line 7 will make it not crash with the Access Violation. However, I highly recommend you read the concurrency chapter in the DPL book: http://www.informit.com/articles/article.aspx?p=1609144
May 05 2012
prev sibling next sibling parent "David Nadlinger" <see klickverbot.at> writes:
On Saturday, 5 May 2012 at 08:45:21 UTC, Tongzhou Li wrote:
 I wrote: http://codepad.org/K4xQOREZ

Besides the other things mentioned, you also need to lock the monitor associated with a condition before notifying/waiting (cf. a recent discussion on the druntime list). David
May 05 2012
prev sibling next sibling parent "Tongzhou Li" <zhangsongcui hotmail.com> writes:
On Saturday, 5 May 2012 at 17:03:00 UTC, Chris Cain wrote:
 On Saturday, 5 May 2012 at 09:28:34 UTC, Tongzhou Li wrote:
 On Saturday, 5 May 2012 at 09:16:04 UTC, Kagamin wrote:
 What's in console?

object.Error: Access Violation ---------------- ---------------- Expected output: 12341234123.... PS: Line 13 should be: write(Idx);

By default, everything is TLS (thread local) ... so, cv[0] will be null in main even though another thread set it to something else. Adding __gshared in front of "Condition[4] cv;" on line 7 will make it not crash with the Access Violation. However, I highly recommend you read the concurrency chapter in the DPL book: http://www.informit.com/articles/article.aspx?p=1609144

Thanks. Now it works fine with gdc, but still prints nothing with dmd. It seems that "cv[0].notify()" on line 29 affects nothing at all when using dmd2 :(
May 05 2012
prev sibling next sibling parent "Tongzhou Li" <zhangsongcui hotmail.com> writes:
On Saturday, 5 May 2012 at 18:46:32 UTC, David Nadlinger wrote:
 On Saturday, 5 May 2012 at 08:45:21 UTC, Tongzhou Li wrote:
 I wrote: http://codepad.org/K4xQOREZ

Besides the other things mentioned, you also need to lock the monitor associated with a condition before notifying/waiting (cf. a recent discussion on the druntime list). David

Why? There's no data races...
May 05 2012
prev sibling next sibling parent "David Nadlinger" <see klickverbot.at> writes:
On Sunday, 6 May 2012 at 04:54:44 UTC, Tongzhou Li wrote:
 Why? There's no data races...

It's part of the protocol/contract/whatever you want to call it of condition variables as implemented in druntime. David
May 06 2012
prev sibling next sibling parent "Chris Cain" <clcain uncg.edu> writes:
On Sunday, 6 May 2012 at 04:52:48 UTC, Tongzhou Li wrote:
 Thanks. Now it works fine with gdc, but still prints nothing 
 with dmd. It seems that "cv[0].notify()" on line 29 affects 
 nothing at all when using dmd2 :(

I'm not exactly sure what's going on for it not to work for you, but here's the code I have (I used stdout.flush() because in some command prompts it won't display until it reaches a new line '\n' character otherwise). Maybe this will work better. https://gist.github.com/5a9b7f611f33ee143880
May 06 2012
prev sibling parent "Tongzhou Li" <zhangsongcui hotmail.com> writes:
On Sunday, 6 May 2012 at 10:39:04 UTC, Chris Cain wrote:
 On Sunday, 6 May 2012 at 04:52:48 UTC, Tongzhou Li wrote:
 Thanks. Now it works fine with gdc, but still prints nothing 
 with dmd. It seems that "cv[0].notify()" on line 29 affects 
 nothing at all when using dmd2 :(

I'm not exactly sure what's going on for it not to work for you, but here's the code I have (I used stdout.flush() because in some command prompts it won't display until it reaches a new line '\n' character otherwise). Maybe this will work better. https://gist.github.com/5a9b7f611f33ee143880

I used stdout.flush() then it works. Thanks.
May 06 2012