Simple Threading question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Am I right to assmune that setting a boolean varialbe is not inherently thread safe? This being the case is there a simple way for me to make it thread safe seeing as SyncLock will not work on a value type and Interlocked.Exchange() only works for Integfers and Single
 
Eldon Ferran de Pol said:
Am I right to assmune that setting a boolean varialbe is not
inherently thread safe?
Yes.

This being the case is there a simple way for
me to make it thread safe seeing as SyncLock will not work on a value
type and Interlocked.Exchange() only works for Integfers and Singles

The thing to do is create another variable (eg padlock) which *just*
serves as the lock:

object padlock = new object();

then:

lock (padlock)
{
// Do something with the boolean variable
}
 
Back
Top