PDA

View Full Version : Thread synchronization, QWaitCondition, and recursiveMutex



RThaden
26th June 2008, 18:26
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:


class ManagerThread : public QThread
{
Q_OBJECT
public:
ManagerThread( QObject * parent = 0 ) : QThread( parent ) {}
protected:
void run() { exec(); }
};
...
class XYManager : public QObject
{
Q_OBJECT
QList<int> list;
mutable QMutex* mutex;
public:
XYManager(void);
QList<int> GetList() const {
QMutexLocker locker(mutex);
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


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

RThaden
27th June 2008, 18:06
Hi all,

Something like


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

jacek
28th June 2008, 00:01
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.

RThaden
30th June 2008, 15:23
Thanks jacek,

I think I'll write a small program to test the behaviour.

Kind regards,

Rainer