Sharing unlocked, unchanging data between threads?

Jacob Carlborg doob at me.com
Tue Aug 10 11:54:06 PDT 2010


On 2010-08-10 20:40, Jonathan M Davis wrote:
> On Tuesday, August 10, 2010 10:45:03 Chris Williams wrote:
>> I'm writing a fairly large, multithreaded application and some part
>> of it is causing periodic access errors.
>>
>> Say that I have an associative array like:
>>
>> uint[ char[] ] nameToId;
>>
>> If I set all values before I start my threads going and never change
>> anything after that point -- all access is read only -- is there any
>> way that having multiple threads access it at the same time would
>> mess anything up? I'm doing this in several places as locking a
>> read-only data structure doesn't strike me as something which should
>> be necessary. I don't know what D's associative arrays look like
>> internally though, so perhaps it is dangerous to do. It would be a
>> major hassle to port it all over to locking everything, to test if
>> it fixes the problem. So, hopefully someone can say whether this is
>> worth checking.
>
> Well, unless it's declared shared, it's going to be thread-local, and then each
> thread is going to have their own copy. Now, if it were declared shared and you
> never changed it after initially setting all of its values, then you shouldn't
> have any problems as far as I'm aware. Just accessing an associative array
> shouldn't change it.
>
> - Jonathan M Davis

I though immutable was supposed to be implicitly shared with no need for 
locks or read-write barriers (or what they're called).

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list