Multi-thread problem
-
Hi everyone:
Currrently, I'm working with the project that was involved in the technology multi-thread. There is a promblem confused with me. it can be reprocude with minimal demo as the following code snippet://test_a.h #include <QThread> #include <QObject> class Test_A: public QThread { Q_OBJECT public: static Test_A *GetInstance(); ~Test_A(); void run(); public slots: void init(); void exitCtl(); private: Test_A()=default; bool bScaning = false; }; //test_a.cpp Test_A* Test_A::GetInstance() { static Test_A obj; return &obj; } void Test_A::run() { bScaning = true; while (bScaning) { } } void Test_A::exitCtl() { bScaning = false; }
It's well known that the function run will be invked in the another thread. Hence, Is there need for declaring the variable bScaning is of the type bool to std::atomic_bool? Additionally, Is there need for modifing the variable bScaning via std::atomic::store and std::atomic::load?
Thanks for everyone who provide a feedback for this problem in advance!
-
@Silent_Alex said in Multi-thread problem:
Hence, Is there need for declaring the variable bScaning is of the type bool to std::atomic_bool?
Yes
Additionally, Is there need for modifing the variable bScaning via std::atomic::store and std::atomic::load?
No, assigning will be enough here.
-
@Christian-Ehrlicher Thank, Bro! 😀