www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Assoc Arrays

reply "Bottled Gin" <gin bottled.com> writes:
Hello All

Are associative arrays in D thread-safe. Is it safe to access 
(read-write) a shared assoc array from multiple threads?

I assume they are not, since an assoc array is initialized to 
null. Is that going to change in future?

Regards
- Puneet
Jul 23 2013
parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Tuesday, July 23, 2013 13:34:36 Bottled Gin wrote:
 Hello All
 
 Are associative arrays in D thread-safe. Is it safe to access
 (read-write) a shared assoc array from multiple threads?
Unlike C/C++/Java/etc., by default, variables in D are thread-local. It's variables that are marked with the attributed "shared" which are shared across threads. So, you don't have to worry about thread safety unless you're dealing with variables which are shared, and in that case, AAs definitely aren't thread-safe on their own. You need to protect access to them with mutexes or synchronized blocks or the like.
 I assume they are not, since an assoc array is initialized to
 null. Is that going to change in future?
I'm afraid that I don't see what initializing to null has to do with thread- safety. And while I'm sure that the implementation of AAs will be improved in the future, I don't expect how they fundamentally work to change. - Jonathan M Davis
Jul 23 2013