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.