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. Two QThreads and one static function cause havoc
Forum Updated to NodeBB v4.3 + New Features

Two QThreads and one static function cause havoc

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 1.9k Views 2 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.
  • J Offline
    J Offline
    Jabber
    wrote on last edited by
    #1

    Surprisingly I have gotten the QThread example in the Qt documentation to actually work. (I only wish they had clearly indicated the use of "emit operate(QString("operate signal"));" to make it all dance).

    To demonstrate that my two threads are working I have this code :

    in the main app :
    void BigFatApp::runController() {

    controller = new Controller();
    controller->doIt();
    

    }

    in the Controller and Worker classes :

    void Controller::doIt() {

    workerThread.start();
    
    emit operate(QString("operate signal from doIt()"));  // make the workerThread dance
    
    QThread::sleep(2.0);
    std::cout << "Controller::doIt() after 2 sec" << std::endl;
    
    QThread::sleep(2.0);
    std::cout << "Controller::doIt() after 4 sec" << std::endl;
    

    }

    and :

    *void Worker::doWork(const QString &parameter) {

    /* ... here is the expensive or blocking operation ... */

    std::cout << "Worker::doWork() " + parameter.toStdString() << std::cout;
    
    QThread::sleep(2.0);
    std::cout << "Worker::doWork after 2 sec" << std::endl;
    
    QThread::sleep(2.0);
    std::cout << "Worker::doWork after 4 sec" << std::endl;
    
    QString result("result from Worker::doWork()");
    emit resultReady(result);
    

    }

    However when I use K::show(std::string), a static function instead of std::cout, then my whole app locks up. Why?

    And BTW, is QThread::sleep(1.0) the best way to introduce a time delay?

    J 1 Reply Last reply
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hello and welcome!

      What is K::show(std::string) and what/how is it doing?

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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mcosta
        wrote on last edited by
        #3

        HI and welcome to devnet,

        And BTW, is QThread::sleep(1.0) the best way to introduce a time delay?

        depends why you need a time delay. usually synchronization with delays is not a good idea.

        Once your problem is solved don't forget to:

        • Mark the thread as SOLVED using the Topic Tool menu
        • Vote up the answer(s) that helped you to solve the issue

        You can embed images using (http://imgur.com/) or (http://postimage.org/)

        J 1 Reply Last reply
        0
        • J Offline
          J Offline
          Jabber
          wrote on last edited by
          #4

          K.h

          class K
              ...
          static void show(std::string);
              ...
          
          K.cpp
          
              ....
          void K::showConsole(std::string s) {
          
          	std::cout << s << std::endl;
          }
              ...
          
          1 Reply Last reply
          0
          • M mcosta

            HI and welcome to devnet,

            And BTW, is QThread::sleep(1.0) the best way to introduce a time delay?

            depends why you need a time delay. usually synchronization with delays is not a good idea.

            J Offline
            J Offline
            Jabber
            wrote on last edited by
            #5

            @mcosta
            I'm not synchronising anything.
            I'll be generating a variable takt mechanism
            for controlling animation on a panel.

            1 Reply Last reply
            0
            • J Jabber

              Surprisingly I have gotten the QThread example in the Qt documentation to actually work. (I only wish they had clearly indicated the use of "emit operate(QString("operate signal"));" to make it all dance).

              To demonstrate that my two threads are working I have this code :

              in the main app :
              void BigFatApp::runController() {

              controller = new Controller();
              controller->doIt();
              

              }

              in the Controller and Worker classes :

              void Controller::doIt() {

              workerThread.start();
              
              emit operate(QString("operate signal from doIt()"));  // make the workerThread dance
              
              QThread::sleep(2.0);
              std::cout << "Controller::doIt() after 2 sec" << std::endl;
              
              QThread::sleep(2.0);
              std::cout << "Controller::doIt() after 4 sec" << std::endl;
              

              }

              and :

              *void Worker::doWork(const QString &parameter) {

              /* ... here is the expensive or blocking operation ... */

              std::cout << "Worker::doWork() " + parameter.toStdString() << std::cout;
              
              QThread::sleep(2.0);
              std::cout << "Worker::doWork after 2 sec" << std::endl;
              
              QThread::sleep(2.0);
              std::cout << "Worker::doWork after 4 sec" << std::endl;
              
              QString result("result from Worker::doWork()");
              emit resultReady(result);
              

              }

              However when I use K::show(std::string), a static function instead of std::cout, then my whole app locks up. Why?

              And BTW, is QThread::sleep(1.0) the best way to introduce a time delay?

              J Offline
              J Offline
              Jakob
              wrote on last edited by
              #6

              @Jabber Which example? I know of a whole bunch of them

              1 Reply Last reply
              0
              • T Offline
                T Offline
                topse
                wrote on last edited by
                #7

                OK... we know much to less, to help you here. Using a static function in general does not lock up anything. BTW why mix up std and Qt? Use qDebug()?

                Tobias

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  Jabber
                  wrote on last edited by
                  #8

                  @topse said:

                  BTW why mix up std and Qt

                  with std::string I can concatenate with '+' and I've never used qDebug before. I'm not working in the Qt environment with this thing.
                  The example? http://doc.qt.io/qt-5/qthread.htmllink text

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    topse
                    wrote on last edited by
                    #9

                    std::cout << "Controller::doIt() after 2 sec" << std::endl;

                    ==

                    qDebug() << "Controller::doIt() after 2 sec";

                    You can use + and << operators.

                    And sorry again: with the given Information I cant see what you are trying and so cant give more help :-/ Why you need a time delay in a worker thread?

                    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