stop_token


  1. Stop a loop early:

for (int i = 0; i < 10; ++i) {
  if (stop_token.stop_requested()) {
    break;
  }
  // Do something
}
  1. Stop a task before completion:

auto task = std::async(std::launch::async, []() {
  // Do something
  if (stop_token.stop_requested()) {
    return;
  }
  // Do something else
});

// Later...
if (need_to_stop) {
  stop_token.request_stop();
}
  1. Gracefully handle cancellation in a thread:

  1. Stop a long-running computation:

  1. Stop an HTTP request:

  1. Stop a file download:

  1. Stop a video stream:

  1. Stop an audio stream:

  1. Stop a web socket connection:

  1. Stop a database query:

  1. Stop a coroutine:

  1. Stop an actor:

  1. Stop a timer:

  1. Stop a sleep:

  1. Stop a wait on a condition variable:

  1. Stop a wait on a semaphore:

  1. Stop a wait on a barrier:

  1. Stop a wait on a future:

  1. Stop a wait on a promise:

  1. Stop a wait on a channel:

  1. Stop a wait on a port: