【发布时间】:2013-08-16 05:05:07
【问题描述】:
我的程序有 2 个线程和一个 int 全局变量。一个线程正在读取该变量,而另一个线程正在写入该变量。在这种情况下我应该使用互斥锁吗?
这些函数在我的程序中同时从 2 个线程重复执行。
void thread1()
{
if ( condition1 )
iVariable = 1;
else if ( condition2 )
iVariable = 2;
}
void thread2()
{
if ( iVariable == 1)
//do something
else if ( iVarable == 2 )
//do another thing
}
【问题讨论】:
标签: multithreading thread-safety mutex