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;
}