[dmd-concurrency] Practical use of shared qualifier?

Sean Kelly sean at invisibleduck.org
Tue Mar 15 09:50:09 PDT 2011


The compiler should, but it doesn't yet. For the next release I'm going to expose atomicLoad and atomicStore to deal with this until shared is fully implemented. 

Sent from my iPhone

On Mar 9, 2011, at 9:05 AM, "Robert Jacques" <sandford at jhu.edu> wrote:

> Hi Puneet,
> This list was used to discuss the creation of D's concurrency model and is basically dead at this point. I'd strongly suggest posting to the D.learn or D newsgroups, instead. Regarding your specific point, yes, the compiler should insert the correct memory barriers for shared variables. The issue with regard to methods that you are seeing indicates that you are not declaring your classes either as shared or synchronized; I wasn't aware it was still possible to declare a non-shared class as shared. Anyways, the proper way to declare a class is either:
> 
> shared A {}
> 
> or
> 
> synchronized B {}
> 
> which will cause all the methods of A/B to be declared as shared/synchronized, respectively. shared methods allow you to manually control synchronization blocks and/or do lock-free programming, etc. The container library is still under development and doesn't contain concurrent containers yet. (implementations are welcome)
> 
> 
> On Mon, 07 Mar 2011 20:19:42 -0500, d coder <dlang.coder at gmail.com> wrote:
> 
>> Greetings
>> 
>> I lately came across usefulness of "shared" qualifier in section 13.12.1.
>> After reading the section, I guess I need to use shared qualifier more
>> frequently than to just make sure that global variables are not treated
>> thread-local. I think I need to make all class objects that would be
>> accessed by multiple threads shared as well. Adding shared qualifier to
>> these variables would make sure that the compiler adds memory barriers
>> wherever applicable and that the compiler maintains sequential consistency
>> for these objects. Am I right?
>> 
>> I tried to follow this on the application I am developing. Note that I am
>> using threads from std.thread library (not MPI from std.concurrency). I
>> started by adding shared qualifier on the variable declarations that I know
>> would be accessed by multiple threads. To my surprise the compiler forces me
>> to declare all the functions that take shared variables as parameters as
>> synchronized. Since this applies to shared "this" objects as well, I am
>> being forced to declare practically every method/function in my code as
>> synchronous. While I understand that the compiler is trying to enforce such
>> type checking to ensure mutex locking on shared objects, it is turning out
>> to be pretty much impractical to use in my case. I would have preferred
>> using synchronized code blocks for my critical sections instead of making
>> all my functions synchronized.
>> 
>> Another issue is that the std container library does not seem to work with
>> shared objects. So I have to do a lot of explicit casting while using this
>> library with the objects that I have qualified as shared.
>> 
>> Kindly let me know if I am missing something. I tried google code search and
>> could not find anybody using much of shared qualifier. While I know the
>> number of shared objects has to be minimized by design, the nature of my
>> application is such that I am forced to use std.thread library and
>> am constrained to have lots of shared objects.
>> 
>> I am kind of stuck right now. Will appreciate any inputs.
>> 
>> Regards
>> - Puneet
> _______________________________________________
> dmd-concurrency mailing list
> dmd-concurrency at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/dmd-concurrency


More information about the dmd-concurrency mailing list