bool Associative Array Synchronized

Etienne etcimon at gmail.com
Thu Mar 20 11:06:18 PDT 2014


On 2014-03-20 1:52 PM, Steven Schveighoffer wrote:
> On Thu, 20 Mar 2014 13:43:58 -0400, Etienne <etcimon at gmail.com> wrote:
>
>> I'd like to "cowboy it" on an AA that looks like this:
>>
>> __gshared bool[string] m_mutex;
>>
>> I think it'll be much faster for my code because this AA could need to
>> be checked and switched possibly millions of times per second and I
>> wouldn't want to slow it down with a mutex protecting it.
>>
>> I'm thinking the bool would be synchronized at the hardware level
>> anyway, since it's just a bit being flipped or returned. Am I right to
>> assume this and (maybe an assembly guru can answer) it safe to use?
>
> No, it's not safe. With memory reordering, there is no "safe" unless you
> use mutexes or low-level atomics.
>
> Long story short, the compiler, the processor, or the memory cache can
> effectively reorder operations, making one thread see things happen in a
> different order than they are written/executed on another thread. There
> are no guarantees.
>
> -Steve

Right, I was assuming it was always ordered, but modern processor 
pipelines are different I guess.


More information about the Digitalmars-d-learn mailing list