[Qt-interest] How to block until posted event is processed?

Ross Bencina rossb-lists at audiomulch.com
Mon May 31 11:17:39 CEST 2010


I know this is possibly too close to "cross-wired wait conditions", but how 
about:

class WorkFinishedEvent: QEvent{
    WorkFinishedEvent( QWaitCondition *recievedCondition )
        : recievedCondition_(recievedCondition ) {}

    ~WorkFinishedEvent()
    {
        recievedCondition_->wakeOne();
    }
};

void doWorkFromThreadAAA()
{
    // ... work, work ...

    QWaitCondition recievedCondition;
    QApplication::postEvent( objectOnThreadBBB, new 
WorkFinishedEvent(&recievedCondition) );

    //* NOW BLOCK UNTIL BBB HAS PROCESSED THE POSTED EVENT *//

    QMutex mutex; mutex.lock();
    recievedCondition.wait(&mutex);
}

Obviously variations are possible if you want to trigger the wake at a 
different time.

HTH

Ross.



From: "Mihail Naydenov" <mlists at ymail.com>
To: <qt-interest at trolltech.com>
Sent: Monday, May 31, 2010 7:02 PM
Subject: [Qt-interest] How to block until posted event is processed?


> Hi, Im trying to achieve this scenario;
>
> void doWorkFromThreadAAA()
> {
>    // ... work, work ...
>
>   QApplication::postEvent( objectOnThreadBBB, new WorkFinishedEvent);
>
>   //* NOW BLOCK UNTIL BBB HAS PROCESSED THE POSTED EVENT *//
> }
>
>
> I have tried two ways:
> The noob one: sendEvent() - Which crashed with failed assertion about 
> sending events across threads.
> Sidenote: This is not documented!!!
>
> The second one:
> QAbstractEventDispatcher::instance(objectOnThreadBBB->thread())->processEvents(QEventLoop::AllEvents)
>
> This one however crashed with the very same assertion.
>
> My question is: Is the above scenario possible (without some cross-wired 
> wait conditions), considering that "posted events are delived when control 
> returns to the event loop" ... can I get around this?
>
> Thank You
> MihailNaydenov
>
>
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest 




More information about the Qt-interest-old mailing list