Two QThreads and one static function cause havoc



  • 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?


  • Moderators

    Hello and welcome!

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



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



  • K.h

    class K
        ...
    static void show(std::string);
        ...
    
    K.cpp
    
        ....
    void K::showConsole(std::string s) {
    
    	std::cout << s << std::endl;
    }
        ...


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



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



  • 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



  • @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



  • 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?


Log in to reply
 

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