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 ¶meter) {
/* ... 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?
-
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.
-
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 ¶meter) {
/* ... 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?
-
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?