Assoc Array for Concurrency

ZombineDev via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Feb 29 09:38:11 PST 2016


On Monday, 29 February 2016 at 12:43:39 UTC, Chris wrote:
> What's the best way to make an assoc array fit for 
> multi-threading? If this is not possible what would be the best 
> alternative?
>
> Say, for example, `data` is used by a class that is globally 
> accessible to all threads. E.g. like this:
>
> string[string] data;  // defined somewhere
>
> public string getData(string key)
> {
>   if (key in data)
>     return data[key];
>   else
>     return "";
> }

I'm almost sure that built-in AAs don't provide automatic 
synchronization (in my tests I hit a deadlock), but you can 
easily wrap the AA into a struct that does the necessary 
synchronization. However be aware that even then you are not 
going to be safe, because more than one thread can try to access 
the keys or the values of the AA. Also note that because the 
built-in AAs rely on the GC, you may get poor scaling because 
every memory allocation can potentially take the global GC lock, 
which will block all threads from doing any work. So do your own 
tests and if you find the need to improve the performance, I 
would suggest investigating replacing the built-in AA with (for 
example) the hashmap from 
https://github.com/economicmodeling/containers in combination 
with a thread-local allocator.

Here's an example of how to wrap an AA into a moderately safe 
accessor. In the following example I create an additional thread 
which concurrently adds odd numbers into the AA, while the main 
threads add even nubmers:

http://dpaste.dzfl.pl/06025e6374eb

It segfaults on DPaste because you can't create threads there, 
however it works ok on my machine. The output look like this:
"0" : 0.140450140112896
"1" : 1.140450129700608
"2" : 2.140450140112896
"3" : 3.140450129700608
"4" : 4.140450140112896
"5" : 5.140450129700608
"6" : 6.140450140112896
"7" : 7.140450129700608
...


More information about the Digitalmars-d-learn mailing list