<p>Is accessing a <strong>bool</strong> field atomic in C#? In particular, do I need to put a lock around:</p>

<pre><code>class Foo
{
   private bool _bar;

   //... in some function on any thread (or many threads)
   _bar = true;

   //... same for a read
   if (_bar) { ... }
}
</code></pre>

[{'answer_id': 59430, 'author': 'Larsenal', 'author_id': 337, 'author_profile': 'https://Stackoverflow.com/users/337', 'pm_score': 8, 'selected': True, 'text': '<p><strong>Yes.</strong></p>\n\n<blockquote>\n  <p>Reads and writes of the following data types are atomic: bool, char, byte, sbyte, short, ushort, uint, int, float, and reference types. </p>\n</blockquote>\n\n<p>as found in <a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/variables#atomicity-of-variable-references" rel="noreferrer">C# Language Spec</a>.</p>\n\n<p>Edit: It\'s probably also worthwhile understanding the <a href="https://msdn.microsoft.com/en-us/library/x13ttww7(v=vs.140).aspx" rel="noreferrer"><strong>volatile</strong></a> keyword.</p>\n'}, {'answer_id': 59505, 'author': 'McKenzieG1', 'author_id': 3776, 'author_profile': 'https://Stackoverflow.com/users/3776', 'pm_score': 5, 'selected': False, 'text': "<p>bool accesses are indeed atomic, but that isn't the whole story. </p>\n\n<p>You don't have to worry about reading a value that is 'incompletely written' - it isn't clear what that could possibly mean for a bool in any case - but you do have to worry about processor caches, at least if details of timing are an issue. If thread #1 running on core A has your <code>_bar</code> in cache, and <code>_bar</code> gets updated by thread #2 running on another core, thread #1 will not see the change immediately unless you add locking, declare <code>_bar</code> as <code>volatile</code>, or explicitly insert calls to <code>Thread.MemoryBarrier()</code> to invalidate the cached value.</p>\n"}, {'answer_id': 106301, 'author': 'Dror Helper', 'author_id': 11361, 'author_profile': 'https://Stackoverflow.com/users/11361', 'pm_score': 6, 'selected': False, 'text': '<p>As stated above, <code>bool</code> is atomic, but you still need to remember that it also depends on what you want to do with it.</p>\n<pre><code>if(b == false)\n{\n    //do something\n}\n</code></pre>\n<p>is not an atomic operation, meaning that the value of <code>b</code> could change before the current thread executes the code after the <code>if</code> statement.</p>\n'}, {'answer_id': 16664812, 'author': 'stux', 'author_id': 1581640, 'author_profile': 'https://Stackoverflow.com/users/1581640', 'pm_score': 2, 'selected': False, 'text': '<p>the approach I have used, and I think is correct, is</p>\n\n<pre><code>volatile bool b = false;\n\n.. rarely signal an update with a large state change...\n\nlock b_lock\n{\n  b = true;\n  //other;\n}\n\n... another thread ...\n\nif(b)\n{\n    lock b_lock\n    {\n       if(b)\n       {\n           //other stuff\n           b = false;\n       }\n     }\n}\n</code></pre>\n\n<p>the goal was basically to avoid having to repetively lock an object on every iteration just to check if we needed to lock it in order to provide a large amount of state change information which occurs rarely. I <em>think</em> this approach works. And if absolute consistancy is required, I <em>think</em> volatile would be appropriate on the b bool.</p>\n'}]
['https://Stackoverflow.com/questions/59422', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/838/']