I am very confused about this subject - whether reading/toggling a bool value is thread-safe.
// case one, nothing private bool v1; public bool V1 { get { return v1; } set { v1 = value; } } // case two, with Interlocked on set private int v2; public int V2 { get { return v2; } set { Interlocked.Exchange(ref v2, value); } } // case three, with lock on set private object fieldLock = new object(); private bool v3; public bool V3 { get { return v3; } set { lock (fieldLock) v3 = value; } }
Are all of them thread-safe?
EDIT
From what I have read (click) atomicity of bool does not guarantee it will be thread safe. Will then volatile
type help?
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
A little bit late but should be useful to the others.
You can implement your own thread safe boolean in the following way:
// default is false, set 1 for true. private int _threadSafeBoolBackValue = 0; public bool ThreadSafeBool { get { return (Interlocked.CompareExchange(ref _threadSafeBoolBackValue, 1, 1) == 1); } set { if (value) Interlocked.CompareExchange(ref _threadSafeBoolBackValue, 1, 0); else Interlocked.CompareExchange(ref _threadSafeBoolBackValue, 0, 1); } }
Be sure to use Property everywhere, never access int
variable directly.
No, not all of them are thread safe.
Case one isn't actually completely thread safe, or better saying - it isn't thread safe at all. Even if operations with boolean are atomic, variable value can be stored in a cache, and so, as in multicore CPU each core has it's own cache, value can be potentially corrupted.
Going even further, compiler and CPU can perform some internal optimizations, including instruction reordering, which can harmfully affect your program's logic.
You can add the volatile
keyword, to notify the compiler that this field is used in a multi-threaded context. It will fix problems with cache and instruction reordering, but doesn't give you truly "thread safe" code (as write operations still will be not synchronized). Also volatile
cannot be applied to local variable.
So when dealing with multi-threading you always have to use some technique of thread synchronization on valuable resources.
For more information - read this answer, which has some deeper explanation of different techniques. (example there is about int
, but is doesn't really matter, it describes general approach.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With