Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If a method (instance or static) only references variables scoped within that method then it is thread safe because each thread has its own stack:</p> <p>In this instance, multiple threads could call <code>ThreadSafeMethod</code> concurrently without issue.</p> <pre><code>public class Thing { public int ThreadSafeMethod(string parameter1) { int number; // each thread will have its own variable for number. number = parameter1.Length; return number; } } </code></pre> <p>This is also true if the method calls other class method which only reference locally scoped variables:</p> <pre><code>public class Thing { public int ThreadSafeMethod(string parameter1) { int number; number = this.GetLength(parameter1); return number; } private int GetLength(string value) { int length = value.Length; return length; } } </code></pre> <p>If a method accesses any (object state) properties or fields (instance or static) then you need to use locks to ensure that the values are not modified by a different thread.</p> <pre><code>public class Thing { private string someValue; // all threads will read and write to this same field value public int NonThreadSafeMethod(string parameter1) { this.someValue = parameter1; int number; // Since access to someValue is not synchronised by the class, a separate thread // could have changed its value between this thread setting its value at the start // of the method and this line reading its value. number = this.someValue.Length; return number; } } </code></pre> <p>You should be aware that any parameters passed in to the method which are not either a struct or immutable could be mutated by another thread outside the scope of the method.</p> <p>To ensure proper concurrency you need to use locking.</p> <p>for further information see <a href="http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx" rel="noreferrer">lock statement C# reference</a> and <a href="http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.aspx" rel="noreferrer">ReadWriterLockSlim</a>. </p> <p><em>lock</em> is mostly useful for providing one at a time functionality, <br/> <code>ReadWriterLockSlim</code> is useful if you need multiple readers and single writers.</p>
    singulars
    1. This table or related slice is empty.
    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