Why is QMutex used in this code?
I was reading someone else's code and I encountered this piece of code that is part of a multi - threaded application .This code is part of the run()
function in a class which is inherited from QThread .
I thought QMutex is used for protecting a variable from being changed by several threads simultaneously . But here stopped
is a member of the class. So it will be created for each instance of the class.
Am I wrong about mutexes? Has the programmer written a wrong code :) ? Or here mutex has another application?
void aThread::run(){
aMutex.lock();
if (stopped)
{
stopped=false;
aMutex.unlock();
break;
}
aMutex.unlock();
}
Here is the declaration of the stopped :
class aThread : public QThread{
public :
void run();
private:
volatile bool stopped;
}
链接地址: http://www.djcxy.com/p/51222.html
上一篇: 多线程安全地插入矢量的最快方式是什么?
下一篇: 为什么在这个代码中使用QMutex?