bool Associative Array Synchronized

Steven Schveighoffer schveiguy at yahoo.com
Thu Mar 20 10:52:57 PDT 2014


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


More information about the Digitalmars-d-learn mailing list