views:

480

answers:

4

For example, do i need to lock 'bool' value when doing multithreading?

+1  A: 

Similar Question here

For the definitive answer go to the spec. :)

Partition I, Section 12.6.6 of the CLI spec states: "A conforming CLI shall guarantee that read and write access to properly aligned memory locations no larger than the native word size is atomic when all the write accesses to a location are the same size."

So that confirms that s_Initialized will never be unstable, and that read and writes to primitve types are atomic.

Interlocking creates a memory barrier to prevent the processor from reordering reads and writes. The lock creates the only required barrier in this example.

John.

Essentially, you wont have a "crash" problem from not locking a bool. What you may have is a race condition for the order of which the bool is updated or read. If you want to garuntee that the bool is written to/read from in a specific order, then you'd want to use some sort of locking mechanism.

NebuSoft
+1  A: 

Static primitive types are threadsafe, so you don't need to lock those typed variables. However, any instance variable of a primitive type is not guaranteed to be. See here: Are primitive types like bool threadsafe ?

MSDN PrimitiveType Class

And here's another useful link that might also be of interest which I find the solution very compelling: SO Question: How do I know if a C# method is thread safe?

Will Marcouiller
+1  A: 

Sort of. There's an excellent thread about this here, but the short version is, while a given read or write may be atomic, that's almost never what you're doing. For example, if you want to increment an integer, you need to 1) read the value, 2) add one to the value and 3) store the value back. Any of those operations can be interrupted.

That's the reason for classes such as "Interlocked".

jvenema
+3  A: 

There is no such thing as an atomic type. Only operations can be atomic.

Reading and writing a data type that fits into a single word (int on a 32-bit processor, long on a 64-bit processor) is technically "atomic", but the jitter and/or processor can decide to reorder instructions and thus create unexpected race conditions, so you either need to serialize access with lock, use the Interlocked class for writes (and in some cases reads), or declare the variable volatile.

The short answer is: If two different threads may access the same field/variable and at least one of them will be writing, you need to use some sort of locking. For primitive types that's generally the Interlocked class.

Aaronaught
Yes an operation can be atomic or not, but there are specific data types that are referred to as being atomic types because they allow atomic operations due to their memory size requirements in regards to the word size on the system.
NebuSoft
@NebuSoft: Referred to by whom or what? Certainly not by the C# specification. See section 5.5, *Atomicity of variable references*, which specifically refers to the atomicity of *reads* and *writes* - not of the types themselves. Every type can be made to allow specific atomic operations, and it only takes two lines of code to perform a non-atomic operation on a primitive type.
Aaronaught
Sorry I was referring to the terms in general:http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/atomic/package-summary.htmlhttp://www-2.cs.cmu.edu/afs/cs/project/pscico/doc/nesl/manual/node14.htmlhttp://publib.boulder.ibm.com/infocenter/idshelp/v10/index.jsp?topic=/com.ibm.ddi.doc/ddi160.htmhttp://download.oracle.com/docs/cd/B28359_01/gateways.111/b31053/ims_datatype_conver.htmI'm simply pointing out that it is common to refer to a variable as being atomic based on type in a general sense.
NebuSoft
I'm not disagreeing with your answer, I'm simply bothered by the phrase "There is no such thing as an atomic type" although per C# specification, I suppose that is an accurate statement. I work in C# and many other languages so I often think in reference to the concept itself, not specific to an implementation.
NebuSoft
@NebuSoft: Fair enough but I would add that in all of the above cases except the Java one, "atomic" doesn't necessarily mean "thread safe", it just means "not composed of any other any other smaller types." The notion of an "atomic type" in the context of thread-safety is really a Java-specific term, sort of like a "concurrent collection" in .NET.
Aaronaught