Thread synchronization, QWaitCondition, and recursiveMutex
Hi all,
just need a hint from somebody.
I have several classes derived from QObject acting as some kind of "managers" with signals and slots. Each of this objects runs in its own thread. This way, it's easy to serialize signals by using queued signal/slots connections. Inside the objects, there is a QList which has to be protected by a mutex.
Something like this:
Code:
class ManagerThread
: public QThread{
Q_OBJECT
public:
protected:
void run() { exec(); }
};
...
{
Q_OBJECT
QList<int> list;
public:
XYManager(void);
QList<int> GetList() const {
return listNamesProgInfo;
};
public Q_SLOTS:
void initialize();
void addToList(int a);
Q_SIGNALS:
void signal_ListChanged();
};
...
xy_manager = new XYManager(*this);
threadXY = new ManagerThread();
threadXY->start();
xy_manager->moveToThread(threadXY);
Now I can e.g. invoke a slot either with QMetaObject::invokeMethod or with a signal/slot connection in xy_manager which performs some action with a queued connection.
So far, so good. But: if I want the thread which invokes the method in the manager to wait for completion of the method, how can I achieve that?
Something like
Code:
QMetaObject::invokeMethod(xy_manager,
"initialize", Qt
::BlockingQueuedConnection);
// wait here until initialize has finished.
As far as I know, a BlockingQueuedConnection waits until the signal is delivered but not until the method has finished.
So, an idea might be to use a QWaitCondition. But: how do I wait for the condition? Does the manager has to make the QWaitCondition available then via a get-method? And further: Since several methods inside the manager access the list, I use a recursive mutex to avoid a deadlock inside the manager. As far as I understand, recursive mutexes don't work with QWaitConditions, right?
I am a bit puzzled on this one. Any help?
Thanks,
Rainer
Re: Thread synchronization, QWaitCondition, and recursiveMutex
Quote:
Originally Posted by
RThaden
Hi all,
Something like
Code:
QMetaObject::invokeMethod(xy_manager,
"initialize", Qt
::BlockingQueuedConnection);
// wait here until initialize has finished.
Rainer
Not sure about that: does the invokeMethod above with the BlockingQueuedConnection return when the signal has been delivered or when the desired slot has been fully executed? Last option would solve my problem.
Kind regards,
Rainer
Re: Thread synchronization, QWaitCondition, and recursiveMutex
Quote:
Originally Posted by
RThaden
Not sure about that: does the invokeMethod above with the BlockingQueuedConnection return when the signal has been delivered or when the desired slot has been fully executed?
The blocking is done with a semaphore which is released in QMetaCallEvent destructor, so the sender is blocked as long as the meta call event object exists. AFAIK when event loop picks up the meta call event it first executes the slot and then deletes the event.
Re: Thread synchronization, QWaitCondition, and recursiveMutex
Thanks jacek,
I think I'll write a small program to test the behaviour.
Kind regards,
Rainer