Re: Thread-safety guarantees for basic ops



Hello Mike!

When thinking about thread-safety I've been assuming that the
following are automatically thread-safe without the need for any
explicit locking by the programmer:

- Reading a reference value

This depends on the size of the Reference. Reading Word-Sizes (Int32 on x32)
are thread-safe, but above not (Int64 on x32). Obviously for 64 bit
Platforms, reading Int64 is also threadsafe.

- Writing a reference value

The same.

- Reading/writing built-in simple types sized 32 bits or less (Int32,
Boolean etc)

This depends on the Type.

Less == yes

Boolean: Yes (Normaly the XXX.i1 OpCodes are used for working with bools,
there are no explicit OpCodes for Boolean)
DateTime: No (DateTime is an UInt64 in terms of memory)

I'm slightly worried that I've never seen these assumptions explicitly
confirmed. It's implied in some places - e.g. there's an
Interlocked.Read(ref long) but no Interlocked.Read(ref int) - but I
was hoping someone could point me to an official guarantee in the
docs.

[Interlocked.Read Method (System.Threading)]
http://msdn2.microsoft.com/en-us/library/system.threading.interlocked.read.aspx

I've also been assuming that static constructors are automatically
thread-safe

The Runtime garantuees that the static constructor of any Type will only be
called once. That's all. Of cause it is possible, to write un-threadsafe
code within a cctor.




GP


.



Relevant Pages

  • Thread-safety guarantees for basic ops
    ... explicit locking by the programmer: ... Reading a reference value ... thread-safe, ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Thread-safety guarantees for basic ops
    ... explicit locking by the programmer: ... This depends on the size of the Reference. ... Reading Word-Sizes ... That depends on what you mean by "thread-safe". ...
    (microsoft.public.dotnet.framework.clr)
  • Re: DTS Datapump Column Order Unstable
    ... According to the SQL Online Help I have been reading. ... For this to work I need to reference the Destination fields in a specific ... wizard issue and occurs anytime I reenter the wizard dialog ... when the Source collection order is correct, ...
    (microsoft.public.sqlserver.dts)
  • Re: Why questions dont get answered, or "No, Ive already RTFM, tell me the answer!"
    ... >> My first experience with Linux was when I bought a book about Linux that ... >> contained Red Hat 5. ... > command line before starting a program before reading other ... > while you may need the reference for obscure options later. ...
    (Fedora)
  • Re: Parallelization on muli-CPU hardware?
    ... > You forgot GarbageCollection instead of reference counting. ... data-locking scheme being thread-safe, but even if one used a GC scheme ... that was completely thread-safe regardless of locking, ...
    (comp.lang.python)

Loading