# stop\_token

***

1. **Stop a loop early:**

```cpp
for (int i = 0; i < 10; ++i) {
  if (stop_token.stop_requested()) {
    break;
  }
  // Do something
}
```

2. **Stop a task before completion:**

```cpp
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();
}
```

3. **Gracefully handle cancellation in a thread:**

```cpp
void worker_thread(std::stop_token stop_token) {
  while (!stop_token.stop_requested()) {
    // Do something
  }
}

// In the main thread...
std::thread worker(worker_thread, stop_token);

// Later...
if (need_to_stop) {
  stop_token.request_stop();
  worker.join();
}
```

4. **Stop a long-running computation:**

```cpp
auto computation = std::async(std::launch::async, []() {
  // Perform a long-running computation
  if (stop_token.stop_requested()) {
    throw std::runtime_error("Computation was stopped");
  }
  return result;
});

// Later...
if (need_to_stop) {
  stop_token.request_stop();
  try {
    computation.get();
  } catch (const std::runtime_error& e) {
    // Handle the error
  }
}
```

5. **Stop an HTTP request:**

```cpp
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
curl_easy_setopt(curl, CURLOPT_STOP_TOKEN, &stop_token);
curl_easy_perform(curl);

// Later...
if (need_to_stop) {
  stop_token.request_stop();
}
```

6. **Stop a file download:**

```cpp
std::ifstream file("file.txt");
while (!stop_token.stop_requested() && !file.eof()) {
  // Read from file
}

// Later...
if (need_to_stop) {
  stop_token.request_stop();
}
```

7. **Stop a video stream:**

```cpp
VideoCapture cap("video.mp4");
while (!stop_token.stop_requested() && cap.isOpened()) {
  // Read frame from video
}

// Later...
if (need_to_stop) {
  stop_token.request_stop();
}
```

8. **Stop an audio stream:**

```cpp
AudioCapture cap;
while (!stop_token.stop_requested() && cap.isOpened()) {
  // Read audio data
}

// Later...
if (need_to_stop) {
  stop_token.request_stop();
}
```

9. **Stop a web socket connection:**

```cpp
WebSocketClient client;
client.connect("ws://example.com");
while (!stop_token.stop_requested() && client.isOpen()) {
  // Receive and send messages
}

// Later...
if (need_to_stop) {
  stop_token.request_stop();
  client.close();
}
```

10. **Stop a database query:**

```cpp
std::unique_ptr<sql::Statement> stmt(db.createStatement());
stmt->execute("SELECT * FROM table");
while (!stop_token.stop_requested() && stmt->next()) {
  // Process row
}

// Later...
if (need_to_stop) {
  stop_token.request_stop();
}
```

11. **Stop a coroutine:**

```cpp
coroutine_handle<> coro = coro::create([](coro::stop_token stop_token) {
  // Do something
  if (stop_token.stop_requested()) {
    return;
  }
  // Do something else
});

// Later...
if (need_to_stop) {
  stop_token.request_stop();
  coro.join();
}
```

12. **Stop an actor:**

```cpp
auto actor = actor<void>([](actor::context<> ctx, coro::stop_token stop_token) {
  while (!stop_token.stop_requested()) {
    auto msg = ctx.poll_mailbox();
    if (msg) {
      // Process message
    }
  }
});

// Later...
if (need_to_stop) {
  stop_token.request_stop();
  actor.join();
}
```

13. **Stop a timer:**

```cpp
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
while (!stop_token.stop_requested()) {
  if (std::chrono::steady_clock::now() - start > std::chrono::seconds(10)) {
    // Stop timer
    break;
  }
  // Do something
}

// Later...
if (need_to_stop) {
  stop_token.request_stop();
}
```

14. **Stop a sleep:**

```cpp
while (!stop_token.stop_requested()) {
  std::this_thread::sleep_for(std::chrono::seconds(1));
  // Do something
}

// Later...
if (need_to_stop) {
  stop_token.request_stop();
}
```

15. **Stop a wait on a condition variable:**

```cpp
std::condition_variable cv;
std::mutex mtx;
while (!stop_token.stop_requested()) {
  std::unique_lock<std::mutex> lock(mtx);
  cv.wait(lock, stop_token);
  // Do something
}

// Later...
if (need_to_stop) {
  stop_token.request_stop();
  cv.notify_all();
}
```

16. **Stop a wait on a semaphore:**

```cpp
std::counting_semaphore sem(1);
while (!stop_token.stop_requested()) {
  sem.acquire(stop_token);
  // Do something
  sem.release();
}

// Later...
if (need_to_stop) {
  stop_token.request_stop();
  sem.release();
}
```

17. **Stop a wait on a barrier:**

```cpp
std::barrier barrier(10);
while (!stop_token.stop_requested()) {
  barrier.wait(stop_token);
  // Do something
}

// Later...
if (need_to_stop) {
  stop_token.request_stop();
  barrier.arrive_and_drop();
}
```

18. **Stop a wait on a future:**

```cpp
std::future<void> future = std::async(std::launch::async, []() {
  // Do something
});
while (!stop_token.stop_requested()) {
  if (future.wait_for(std::chrono::seconds(1)) == std::future_status::ready) {
    // Future is ready
    break;
  }
  // Do something else
}

// Later...
if (need_to_stop) {
  stop_token.request_stop();
  future.cancel();
}
```

19. **Stop a wait on a promise:**

```cpp
std::promise<void> promise;
std::future<void> future = promise.get_future();
while (!stop_token.stop_requested()) {
  if (future.wait_for(std::chrono::seconds(1)) == std::future_status::ready) {
    // Promise is ready
    break;
  }
  // Do something else
}

// Later...
if (need_to_stop) {
  stop_token.request_stop();
  promise.set_exception(std::make_exception_ptr(std::runtime_error("Promise was stopped")));
}
```

20. **Stop a wait on a channel:**

```cpp
std::channel<void> channel;
while (!stop_token.stop_requested()) {
  if (channel.wait_for_ready(stop_token)) {
    // Channel is ready
    channel.pop();
  }
  // Do something else
}

// Later...
if (need_to_stop) {
  stop_token.request_stop();
  channel.close();
}
```

21. **Stop a wait on a port:**

```cpp
std::port port;
while (!stop_token.stop_requested()) {
  if (port.wait_for_ready(stop_token)) {
    // Port is ready
    port.pop();
  }
  // Do something else
}

// Later...
if (need_to_stop) {
  stop_token.request_stop();
  port.close();
}
```
