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. how to receive other thread's return value in main thread
Forum Updated to NodeBB v4.3 + New Features

how to receive other thread's return value in main thread

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 2.2k Views
  • 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.
  • N Offline
    N Offline
    name101
    wrote on last edited by
    #1

    testthread::testthread(worker* w, QObject parent):QObject(parent),result(0)
    {
    worker
    _w=w;
    _w->moveToThread(&workerThread);
    connect(&workerThread,SIGNAL(started()),_w,SLOT(dowork()));
    connect(&workerThread,SIGNAL(finished()),_w,SLOT(deleteLater()));
    connect(_w,SIGNAL(finished()),&workerThread,SLOT(deleteLater()));
    connect(_w,SIGNAL(finished()),&workerThread,SLOT(quit()));
    connect(_w,SIGNAL(resultReady(int)),this,SLOT(handleResult(int)));
    workerThread.start();
    }
    int main(int argc, char argv[])
    {
    QCoreApplication a(argc, argv);
    int i=0;
    realworker
    w=new realworker(i);
    testthread t(w,nullptr);
    t.thread()->wait(); //testthread has Qthread member value.
    return a.exec();
    }

    I want to receive result value after handleResult() call in main()
    So I used wait() in main() , but it prints as below:

    QThread::wait: Thread tried to wait on itself

    How can I get the thread return value?

    jsulmJ 1 Reply Last reply
    0
    • N name101

      testthread::testthread(worker* w, QObject parent):QObject(parent),result(0)
      {
      worker
      _w=w;
      _w->moveToThread(&workerThread);
      connect(&workerThread,SIGNAL(started()),_w,SLOT(dowork()));
      connect(&workerThread,SIGNAL(finished()),_w,SLOT(deleteLater()));
      connect(_w,SIGNAL(finished()),&workerThread,SLOT(deleteLater()));
      connect(_w,SIGNAL(finished()),&workerThread,SLOT(quit()));
      connect(_w,SIGNAL(resultReady(int)),this,SLOT(handleResult(int)));
      workerThread.start();
      }
      int main(int argc, char argv[])
      {
      QCoreApplication a(argc, argv);
      int i=0;
      realworker
      w=new realworker(i);
      testthread t(w,nullptr);
      t.thread()->wait(); //testthread has Qthread member value.
      return a.exec();
      }

      I want to receive result value after handleResult() call in main()
      So I used wait() in main() , but it prints as below:

      QThread::wait: Thread tried to wait on itself

      How can I get the thread return value?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @name101 What do you mean by "return value"? Doesn't your worker already return the result as parameter in resultReady(int) signal?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • N Offline
        N Offline
        name101
        wrote on last edited by
        #3

        I want to wait for resultReady(int) to complete and then get it from main thread like C's WaitForSingleObject function

        jsulmJ 1 Reply Last reply
        0
        • O Offline
          O Offline
          ollarch
          wrote on last edited by
          #4

          As "testthread" is a class and you have an object of this class, you can define another signal from "testthread" class and connect it to a main thread slot.

          1 Reply Last reply
          1
          • N name101

            I want to wait for resultReady(int) to complete and then get it from main thread like C's WaitForSingleObject function

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @name101 Why do you want to wait for it? Just connect a slot to it and do what you need to do when the signal is emitted.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1

            • Login

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