In the end, this is how I solved all the problems.
Following the link provided by Phillip , I found out about Glib::signal_timeout() , and this allowed me to completely rethink the concept of parallelism in my code.
Here is what I had to start with:
if(running) return; running = true; static bool firstTime = true; if(firstTime) { XInitThreads(); firstTime=false; } std::function<void()> f = [this] () -> void { while(running) { this->takeStep(); std::this_thread::sleep_for(std::chrono::milliseconds(300)); } }; std::thread(f).detach();
and this can easily be rewritten as:
if(running) return; running = true; runningHandle = Glib::signal_timeout().connect( sigc::mem_fun(*this, &ArtificialIntelligence::takeStep), 300 );
Then I needed to add runningHandle.disconnect(); into my pause function, and everything began to work wonderfully. In fact, the response speed of the GUI has increased.
So, if someone else is trying to make the process "worked and then sleep," this is a much better alternative. Of course, there are applications in which there is no specified periodicity, and then you need to look for some other solution.
source share