main and background thread example
-
what do you mean by check? and no, how should i?
-
Using the top command for example. Valgrind etc.
-
okay, thanks.
one last question, as you can see, on timer's timeout i execute a function. is it possible stop the timer (so the function won't be executed), and then restart it again? -
-
hi again,
i ran valgrind and it says there's a memory leak onexec()
which is inrun()
overridden method. so what's wrong? -
i wanna write a basic thread example. here's the idea:
i have the main event loop. before callingapp.exec()
, i create an object which itself creates an object and puts it in a separate thread for running. i need some way to tell the background thread that main thread has finished (tojoin()
), so i have abool
to indicate that.however, when the application exits, something goes wrong. i'm running it using visual studio and the debugging session won't stop. i don't understand what's wrong. here's the code:
#include <QApplication> #include <QObject> #include <QThread> #include <iostream> #include <thread> #include <QtWidgets/QLabel> #include <atomic> std::atomic<bool> bMainThreadRunning { true }; class ThreadObj { public: void work() { while(bMainThreadRunning) { ++counter; std::this_thread::sleep_for(std::chrono::seconds(5)); } } private: int counter { 0 }; }; class Obj { public: void start() { ThreadObj obj; m_thread = std::thread(&ThreadObj::work, &obj); } void onMainThreadFinished() { bMainThreadRunning = true; m_thread.join(); } private: std::thread m_thread; }; int main(int argc, char *argv[]) { QApplication a(argc, argv); Obj o; o.start(); QWidget w; w.show(); auto ret = a.exec(); o.onMainThreadFinished(); return 0; }
@user4592357 said in main and background thread example:
ThreadObj obj;
m_thread = std::thread(&ThreadObj::work, &obj);obj is allocated on the stack, it will go out of scope and delete itself
-
@user4592357 said in main and background thread example:
ThreadObj obj;
m_thread = std::thread(&ThreadObj::work, &obj);obj is allocated on the stack, it will go out of scope and delete itself
hi, thanks for the reply.
the code has been modified since the first post (see my last code-post), so it's not the problem -
i've read similar articles, saying "you shouldn't subclass
QThread
". in my case i need to do that. -
Out of curiosity, what are you doing that requires to subclass QThread ?
-
nothing requires it. it's just that the whole implementation is done and i don't wanna change everything