I have a single public variable in a class
bool toSwitch = false;
Func_Thread1()
{
if(toSwitch)
{
.....
....
toSwitch = false;
}
}
and
Func_Thread2()
{
toSwitch = true;
}
The Func_Thread1
will get called frequently(2 seconds once) and during certain interval Func_Thread2
will get called(1 minute once) and change the global variable to true. So at the next Func_Thread1
call the if condition will become true.
Does the toSwitch
needs synchronization as it can read and modified by different threads concurrently ..?
Update 2:
What happens in this environment...?
Func_Thread1()
{
if(toSwitch)
{
.....
....
}
else
{
...
}
}
and
Func_Thread2()
{
if(toSwitch)
{
toSwitch = false;
}
else
{
toSwitch = true;
}
}
Does sync needs here , where one thread modifies and other thread reads the value of that global variable .?