Re: Thread-safety guarantees for basic ops
- From: "Günter Prossliner" <g.prossliner/gmx/at>
- Date: Wed, 12 Dec 2007 17:09:28 +0100
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
.
- Follow-Ups:
- Re: Thread-safety guarantees for basic ops
- From: Jon Skeet [C# MVP]
- Re: Thread-safety guarantees for basic ops
- From: Mike Capp
- Re: Thread-safety guarantees for basic ops
- References:
- Thread-safety guarantees for basic ops
- From: Mike Capp
- Thread-safety guarantees for basic ops
- Prev by Date: Thread-safety guarantees for basic ops
- Next by Date: Re: Thread-safety guarantees for basic ops
- Previous by thread: Thread-safety guarantees for basic ops
- Next by thread: Re: Thread-safety guarantees for basic ops
- Index(es):
Relevant Pages
|
Loading