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:
- {
- protected:
- void run();
- public:
- Sample();
- ~ Sample();
- private:
- mutable QMutex mutex;
- bool Connect()
- {
- Disconnect();
- };
- bool Disconnect()
- {
- 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
4 replies
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)
You must log in to post a reply. Not a member yet? Register here!

