KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I'm done implementing a Go program where the human can interrupt at any time the software to order it to play. Basically, I've got an algorithm running in another thread that has at every moment a "Best move" available, that it keeps on improving. My question is : how do I correctly interrupt a thread in an infinite loop? I've tried a few things, and resolved to do this : class MyWorker : public QObject { Q_OBJECT public: MyWorker(); ~MyWorker(); public: ThreadControl * getThreadControl(); public slots: void work(); private: void endOfComputation(); private: ThreadControl * threadControl; } Notice that I don't subclass QThread : class ThreadControl : public QObject { Q_OBJECT public: ThreadControl(); public: bool getAbort(); Parameter getParameter(); void setParameter(Parameter & param); public slots: void setAbort(bool b); private: QMutex mutex; bool abort; Parameter param; }; And, finally, the infinite loop is coded as this : void Myworker::work() { // ... forever { abort = threadControl->getAbort(); if(abort) { break; } // ... } endOfComputation(); } Then, as you can guess, in the main , I regularly call ThreadControl::setAbort(true) Basically, I just keep a pointer to a boolean in the main thread and I toggle it when I want to . (The boolean is encapsulated in ThreadControl so I can properly lock it with a mutex). So far so good, it has worked for me... But it seems disgusting to me! Toggling pointers to booleans sound like ... bad programming to me... The problem is the documentation on the web is mostly (entirely?) about producers a
Tags (comma-separated)
Save Edits
Cancel