Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have a few options.</p> <ul> <li>Do nothing.</li> <li>Make the class immutable.</li> <li>Use a plain old <code>lock</code>.</li> <li>Mark the fields as <code>volatile</code>.</li> <li>Use the <code>Interlocked</code> set of methods.</li> </ul> <p>Since reads and writes to a <code>bool</code> are guaranteed to be atomic then you may not need to do anything. This will very much depend on the nature of the way your class is going to be used. Atomicity does not equal thread-safety. It is only one aspect of it.</p> <p>The ideal solution is to make your class immutable. Immutable classes are generally thread-safe because they cannot be modified by other threads (or at all for that matter). Sometimes this just is not feasible though.</p> <p>My next preference on the list is a plain old <code>lock</code>. The overhead of acquiring and releasing is very minimal. In fact, I think you will find that a <code>lock</code> will beat out a <code>ReaderWriterLockSlim</code> in most situations especially if all you are doing is reading/writing a variable. My own personal tests indicate that the overhead of RWLS is about 5x <em>slower</em> than a <code>lock</code>. So unless the read operations are unusually long and that they significantly outnumber write operations then RWLS will not help.</p> <p>If you are concerned about lock overhead then by all means mark the fields as <code>volatile</code>. Just remember that <code>volatile</code> is not a magic bullet that solves all concurrency issues. It is not intended as a replacement for <code>lock</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload