I just want to do a simple, though thread-safe, boolean test (and set) so:
if(myBoolean==false) //should not lock/wait!
{
myBoolean=true;
.....
}
else
{
....
}
I considered the following (although possibly incorrectly, so please correct me where I misunderstood)
- using the Lock { if(myBoolean)... } construct seems like a overkill way to do it. And, it also locks the thread while it waits for the lock to become free. I don't want this.
- The AutoResetEvent class does have a concept of a boolean state, but it is used to signal another thread which is waiting. So not relevant in my case
- Semaphore class has a notion of a reference count (probably to throttle the amount of access to a resource?). So probably not what I'm after.
- Mutex class. As far as I understood, this is the same principal as the Lock primitive
Anyone have an idea what is the class/construct to do this in an efficient manner?