Something needs to happen with shared, and soon.

Manu turkeyman at gmail.com
Mon Nov 12 10:02:57 PST 2012


On 12 November 2012 04:30, Walter Bright <newshound2 at digitalmars.com> wrote:

> On 11/11/2012 10:46 AM, Alex Rønne Petersen wrote:
>
>> It's starting to get outright embarrassing to talk to newcomers about D's
>> concurrency support because the most fundamental part of it -- the shared
>> type
>> qualifier -- does not have well-defined semantics at all.
>>
>
> I think a couple things are clear:
>
> 1. Slapping shared on a type is never going to make algorithms on that
> type work in a concurrent context, regardless of what is done with memory
> barriers. Memory barriers ensure sequential consistency, they do nothing
> for race conditions that are sequentially consistent. Remember, single core
> CPUs are all sequentially consistent, and still have major concurrency
> problems. This also means that having templates accept shared(T) as
> arguments and have them magically generate correct concurrent code is a
> pipe dream.
>
> 2. The idea of shared adding memory barriers for access is not going to
> ever work. Adding barriers has to be done by someone who knows what they're
> doing for that particular use case, and the compiler inserting them is not
> going to substitute.
>
>
> However, and this is a big however, having shared as compiler-enforced
> self-documentation is immensely useful. It flags where and when data is
> being shared. So, your algorithm won't compile when you pass it a shared
> type? That is because it is NEVER GOING TO WORK with a shared type. At
> least you get a compile time indication of this, rather than random runtime
> corruption.
>
> To make a shared type work in an algorithm, you have to:
>
> 1. ensure single threaded access by aquiring a mutex
> 2. cast away shared
> 3. operate on the data
> 4. cast back to shared
> 5. release the mutex
>
> Also, all op= need to be disabled for shared types.
>

I agree completely the OP, shared is really very unhelpful right now. It
just inconveniences you, and forces you to perform explicit casts (which
may cast away other attributes like const).
I've thought before that what it might be useful+practical for shared to
do, is offer convenient methods to implement precisely what you describe
above.

Imagine a system where tagging a variable 'shared' would cause it to gain
some properties:
Gain a mutex, implicit var.lock()/release() methods to call on either side
of access to your shared variable, and unlike the current situation where
assignment is illegal, rather, assignment works as usual, but the shared
tag implies a runtime check to verify the item is locked when performing
assignment (perhaps that runtime check would be removed in -release for
performance).

This would make implementing the logic you describe above convenient, and
you wouldn't need to be declaring explicit mutexes around the place. It
would also address the safety by asserting that it is locked whenever
accessed.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20121112/b2d5af05/attachment.html>


More information about the Digitalmars-d mailing list