我有两种情况,我不知道是否应该使用QMutex
。我已经多次运行该程序QMutex
并且它没有向我显示任何异常行为。为简单起见,我在这里略读了代码。但为了安全起见,我想知道我是否应该使用QMutex
?
场景#1:
class A : QObject
{
Q_OBJECT
private double **array;//it is initialised in the constructor & is 100x100
slots:
slot1(); //2 Qthreads are created in my main GUI thread along with 2 objects of class A, & by A aobj.movetothread();
slot2(); //& connecting these 2 slots to started() SIGNAL of respective QThread's
//I have multi-threaded my application.
}
A::slot1()
{
double temp = array[i][j];
//some operations on temp
}
A::slot2()
{
double temp = array[i][j];
//some operations on temp
}
注意:array[][]
初始化后内容不变。我只在 2 个线程中访问它的信息。然而,有时两个线程可能同时访问相同的元素!array
情景#2
A::slot1()
{
double temp = somefunc();
array[0][j] = temp;
}
A::slot2()
{
double temp = somefunc();
array[50][j] = temp;
}
注意:在这种情况下,两个线程修改来自同一个数组的元素,但是它们不修改/访问公共元素,即thread1处理前50 行,而thread2处理接下来的 50 行,但它们甚至不访问行彼此的。