mercredi 21 novembre 2018

C++ run asynch function every second

I am developing a C++ application using Qt and I need to run a function asynchronously every second.

The application works this way:

  • the user start a functionallity;
  • the application call the specific function asynchronously letting the user doing something else in the meantime;
  • when the user stops the functionallity, the application stops calling the function.

For other functions I used Qt integrated SLOTS and SIGNALS, like this:

connect(timer, SIGNAL(timeout()), this, SLOT(updateView()));
timer->start(200);

but for this particular function I wanted to use only C++ functionallity like thread, mutex, future, promise and asynch.

I tried something like this:

if(cmd == start) {
    std::future<void> fn = async(std::launch::async, [](){
    // some code here
 });
}

This way, every time the user clicks start the application calls the lambda function.

Now I want that function to be called every second until the user clicks stop without avoiding him to do something else in the meantime.

Can someone help me?

Aucun commentaire:

Enregistrer un commentaire