Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Thread Synchronous problem
Forum Updated to NodeBB v4.3 + New Features

Thread Synchronous problem

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wearilybird
    wrote on last edited by
    #1

    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 .

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • W Offline
        W Offline
        wearilybird
        wrote on last edited by
        #3

        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 .

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MuldeR
            wrote on last edited by
            #5

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

            My OpenSource software at: http://muldersoft.com/

            Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

            Go visit the coop: http://youtu.be/Jay...

            1 Reply Last reply
            0
            • W Offline
              W Offline
              wearilybird
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved