I have a critical section in my application that contains a lot of code: which is the best way to block access in threadMethod:
A) block the whole block:
private object locker = new object();
private void threadMethod()
{
while(true)
{
lock(locker)
{
}
Thread.Sleep(2000);
}
}
B) Use an additional locked access element canWork:
private static object locker = new object();
private bool canWork;
private bool CanWork
{
get { lock(locker) { return this.canWork; } }
set { lock(locker) { this.canWork = value; } }
}
private void threadMethod()
{
while(true)
{
if(CanWork)
{
}
Thread.Sleep(2000);
}
}
and somewhere in the code
CanWork = false;
UGEEN source
share