[dmd-concurrency] Practical use of shared qualifier?

d coder dlang.coder at gmail.com
Mon Mar 7 17:19:42 PST 2011


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/dmd-concurrency/attachments/20110308/0fbd620b/attachment.html>


More information about the dmd-concurrency mailing list