www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Problems with "shared"

reply jython234 <jython234 users.noreply.github.com> writes:
Hello,

I've recently started a project in D, one which I plan to take 
advantage of concurrency. This means I will need to use the 
shared keyword. However, I have been having a few problems:

1. Where do I use the "shared" keyword? I've done things like 
making whole classes shared, which doesn't seem to make the 
member methods shared either. Also this creates a million 
compiler errors when I try to access the constructor from a 
non-shared function.

2. It seems that whenever I use shared I end up doing a ton of 
"unsafe" casts from shared, and to shared and it makes me feel 
uneasy. I don't think I'm supposed to be doing this.

3. Another concern I have is that I plan on using this project to 
introduce other people to D. These people will probably have some 
background with programming (such as Java), but will be new to 
the "systems" programming area. I like D because of how broad it 
can be, but one of the biggest problems it has is the "shared" 
issue with Thread local data. This could be hard for newcomers to 
learn.

Any tips on how I could fix these problems?
Oct 09 2016
next sibling parent cym13 <cpicard openmailbox.org> writes:
On Sunday, 9 October 2016 at 20:57:40 UTC, jython234 wrote:
 Hello,

 I've recently started a project in D, one which I plan to take 
 advantage of concurrency. This means I will need to use the 
 shared keyword. However, I have been having a few problems:

 1. Where do I use the "shared" keyword? I've done things like 
 making whole classes shared, which doesn't seem to make the 
 member methods shared either. Also this creates a million 
 compiler errors when I try to access the constructor from a 
 non-shared function.

 2. It seems that whenever I use shared I end up doing a ton of 
 "unsafe" casts from shared, and to shared and it makes me feel 
 uneasy. I don't think I'm supposed to be doing this.

 3. Another concern I have is that I plan on using this project 
 to introduce other people to D. These people will probably have 
 some background with programming (such as Java), but will be 
 new to the "systems" programming area. I like D because of how 
 broad it can be, but one of the biggest problems it has is the 
 "shared" issue with Thread local data. This could be hard for 
 newcomers to learn.

 Any tips on how I could fix these problems?
Instead of rewriting a book that already exist I'll just say that [1] should answer all your questions. However I disagree with your saying that you'll "have to use shared", D proposes Data Sharing concurency of course but Message Passing concurrency is reccomended [2]. [1]: http://ddili.org/ders/d.en/concurrency_shared.html [2]: http://ddili.org/ders/d.en/concurrency.html
Oct 09 2016
prev sibling parent reply ag0aep6g <anonymous example.com> writes:
On 10/09/2016 10:57 PM, jython234 wrote:
 1. Where do I use the "shared" keyword?
Mainly on data, I'd say. But I'm only dabbling in concurrency stuff.
 I've done things like making
 whole classes shared, which doesn't seem to make the member methods
 shared either.
It does make the methods shared. It doesn't make the class type itself shared, though. That is, you have write "shared" out when instantiating: shared Foo foo = new shared Foo; or auto foo = new shared Foo; or possibly shared Foo foo = new Foo; but not just auto foo = new Foo; or Foo foo = new Foo;
 Also this creates a million compiler errors when I try to
 access the constructor from a non-shared function.
The host function being unshared shouldn't be a problem. Please show code if that's really it. What doesn't work is creating an unshared object of a class that only has a shared constructor. To create both shared and unshared objects, you need either two constructors or a `pure` one. class C { this() pure {} } void main() { auto u = new C; /* works */ auto s = new shared C; /* too */ }
 2. It seems that whenever I use shared I end up doing a ton of "unsafe"
 casts from shared, and to shared and it makes me feel uneasy. I don't
 think I'm supposed to be doing this.
If you're dealing with shared data directly, you're supposed to do that. Ensure exclusive access, then cast shared away and do your stuff. See std.concurrency and std.parallelism for more abstracted concurrency, with less casts.
Oct 09 2016
parent Satoshi <satoshi gshost.eu> writes:
On Sunday, 9 October 2016 at 21:32:23 UTC, ag0aep6g wrote:
 On 10/09/2016 10:57 PM, jython234 wrote:
 1. Where do I use the "shared" keyword?
What doesn't work is creating an unshared object of a class that only has a shared constructor. To create both shared and unshared objects, you need either two constructors or a `pure` one. class C { this() pure {} } void main() { auto u = new C; /* works */ auto s = new shared C; /* too */ }
Why this ins't in doc? :O I didn't know about using pure in this way.
Oct 09 2016