Static problem
Stanislav Blinov
stanislav.blinov at gmail.com
Thu Oct 7 13:18:26 PDT 2010
Bob Cowdery wrote:
>>
>> Could you please post the use case as well? It should matter not from
>> what module you make the calls, as long as those calls are from the
>> same thread. Different threads get different copies of the registry.
> I suspected that might be the case. Yes they are from separate threads.
> No conflict as one writes all the tids and the others only read them.
> What's the best way to fix that?
>
I wouldn't be too quick to state that there is no conflict, as
associative array access/lookup is most probably not an atomic operation.
What I'd propose is either:
1) Create your own lock-free associative array (yup, reinvent the wheel
to introduce AA to the world of 'shared')
2) In this small case it may seem best (though mind that often such
cases do grow up to the point when you still need to rethink design):
Make your associative array __gshared and perform synchronization by
hand, i.e. create a static Mutex (from core.sync.mutex) variable and
initialize it in your CRegistry's static ctor, and then enclose all
access to associative array in synchronized(mutex) {} blocks.
Maybe concurrent-programming-in-D guru may propose simpler solution, but
I don't see another.
P.S. You can get much useful info on this topic in TDPL, the chapter on
concurrency is even available for free:
http://www.informit.com/articles/article.aspx?p=1609144
More information about the Digitalmars-d-learn
mailing list