1

In my application I have a callback and, if a certain event happens, it has to perform certain operations.

void callback()
{
  if(event == true)
  {
    // long processing
    performOperations();
  }
}

Now, the thing is that the callback is called at a 30Hz rate, while the performOperations function can take also ~10 seconds to complete.

After a quick search online I came across std::async. The strategy I want to implement is that if the performOperations is running and a new event happens I want to "kill" (stop nicely) the running thread and start a new one which still launches the performOperations function.

So, something like this:

void callback()
{
  if(event == true)
  {
    // check if there's a running thread
    if(already_running == true)
    {
      // stop nicely the running thread 
      ...
    }

    // long processing
    performOperations();
  }
}

Therefore, my question. Is there a way to know that performOperations is still running and kill it?

Thanks

Federico Nardi
  • 510
  • 7
  • 19
  • Is it possible you share a kill, which you check periodically in performOperation, and each time before calling performOperation, you set it? – PirklW Jul 10 '20 at 08:42
  • Killing a running thread is rarely a good idea. It is very easy to leak resources or leave things in an inconsistent state. It's better to have the thread check regularly whether it should continue. – molbdnilo Jul 10 '20 at 08:54
  • https://stackoverflow.com/questions/12207684/how-do-i-terminate-a-thread-in-c11 – asmmo Jul 10 '20 at 11:10

0 Answers0