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
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top