Thread Synchronous problem



  • i start a thread to handle somethings ,gui wait for the things process .when end , notify the gui thread go run .here is my code :

    @
    class Worker : public QObject
    {
    Q_OBJECT
    public:
    Worker (){m_hEvent = NULL ;};
    public slots:
    void doWork(const QString &parameter) {
    // ...

       setEvent(m_hEvent);
    }
    

    public :
    void SetSyncEvent(Handle hEvent){m_hEvent = hEvent};
    signals:
    void resultReady(const QString &result);
    private:
    Handle m_hEvent ;
    };

    class Controller : public QObject
    {
    Q_OBJECT
    QThread workerThread;
    public:
    Controller() {

        m_hEvent = ::CreateEvent(NULL,FALSE,FALSE,NULL);
    
        Worker *worker = new Worker;
    
        worker->SetEvent(m_hEvent); 
        worker->moveToThread(&workerThread);
        connect(workerThread, &QThread::finished, worker, &QObject::deleteLater);
        connect(this, &Controller::operate, worker, &Worker::doWork);
       // connect(worker, &Worker::resultReady, this, &Controller::handleResults);
         workerThread.start();
    }
    ~Controller() {
        workerThread.quit();
        workerThread.wait();
    }
    

    void Oper()
    {
    emit operate(QString("fsfs"));
    WaitForSingleObject(m_hEvent,INFINITE);
    .......
    }
    public slots:
    // void handleResults(const QString &);
    signals:
    void operate(const QString &);
    private:
    Handle m_hEvent ;
    };
    @

    [edit: please use @ tags for your code, Eddy]

    void main()
    {
    Controller controle ;
    controle.oper();
    }

    but sometimes i found after worker class's function doWork run the last code setevent(m_hevent), the app can not get the event and the WaitForSingleObject(m_hEvent,INFINITE) always in wait state .
    i use vs2008 qt 5.2.1 ,i found the workerThread can not exit the thread ,can anyone tell me why and how can i solved this problem .


  • Lifetime Qt Champion

    Hi,

    There's something not clear here, you are mixing QThread's class with signals and slots as well as windows's native event handling in a blocking fashion.

    What are you exactly trying to achieve ?



  • i try to achieve do something in a thread and at the same time the main thread wait for the thread until the thing end .


  • Lifetime Qt Champion

    So you want to block your GUI thread ? Not a good idea…

    To wait on something you have for example QSemaphore, QWaitCondition etc.



  • [quote author="wearilybird" date="1399689598"]i try to achieve do something in a thread and at the same time the main thread wait for the thread until the thing end .[/quote]

    Wearilybird, you can achieve this all with Qt Signals&Slots, I suppose.

    In your thread class, derived from "QThread":http://qt-project.org/doc/qt-4.8/qthread.html, you do:
    @MyThread::run(void)
    {
    /do some operation that takes a long time here/
    }@

    And in your "main" class you do:
    @//The function to launch the thread
    MyDialog::beginBackgroundOperation(void)
    {
    m_thread = new MyThread();
    connect(m_thread, SIGNAL(finished()), this, SLOT(backgroundOperationDone()), Qt::QueuedConnection);
    m_thread->start();
    }

    //The slot to be called when thread has finished
    MyDialog::backgroundOperationDone(void)
    {
    /* do something when the thread has finished! */
    }@

    So there's no need to wait for anything. This can all be done in an event driven way. Your slot will be invoked as soon as the thread has finished. Then you can fetch the result from the thread or do whatever is required...



  • i has changed my code .Mostly not block gui thread .and use QWaitcondition ans QSemaphore.but in window QWaitcondition ans QSemaphore use event to realize their function.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.