Threading...
-
I have a multi-threaded application with an object that has re-entrant functions. I am trying to make my object thread safe by including a mutex. I am wondering if you can do multiple QMutexLocker locker on one instance of mutex within the same thread. For instance:
@
class Sample : Public QThread
{
protected:
void run();public:
Sample();
~ Sample();private:
mutable QMutex mutex;
bool Connect()
{
QMutexLocker locker(&mutex);Disconnect();
};
bool Disconnect()
{
QMutexLocker locker(&mutex);obj.close();
};
bool ReadRegister();
bool WriteRegister();
}
@notice in my connect function I am calling my Disconnect function that also tries to do a QMutexLocker on global variable mutex.
- Tien
-
bq. I am wondering if you can do multiple QMutexLocker locker on one instance of mutex within the same thread.
Yes. When the mutex is locked, all other functions that require it to be unlocked, will wait (block) till the mutex is unlocked.
Edit: But in your case, you will halt the thread forever. You can not call a function that requires the mutex to be unlocked from within a function that actually locks the mutex unless you unlock the mutex first. But in your case, the mutex is unlocked when the connect() function goes out of scope (the mutex locker is destroyed)
-
is it true i can set the recursive mode for the mutex to recursive for me to continue my process? How can I set the recursive mode? I tried the following but get an error:
@
class test : Public QThread
{
protected:
void run();private:
QMutex mutex;
public :
test()
{
mutex = new QMutex(QMuted::Recursive); // i can´t construct mutex object...
};
~test();}
@