[dmd-concurrency] Practical use of shared qualifier?

Robert Jacques sandford at jhu.edu
Wed Mar 9 09:05:00 PST 2011


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


More information about the dmd-concurrency mailing list