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