opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
stop_token.h
Go to the documentation of this file.
1#pragma once
2
3#include <version> // __cpp_lib_jthread
4
5#ifdef __cpp_lib_jthread
6 #include <stop_token>
7#else
8 #include <atomic>
9 #include <memory>
10 #include <utility>
11#endif
12
13namespace osc::cpp20
14{
15#ifdef __cpp_lib_jthread
16 using stop_token = std::stop_token;
17#else
18 // C++20: std::stop_token
20 public:
21 explicit stop_token(std::shared_ptr<std::atomic<bool>> shared_state) :
22 shared_state_{std::move(shared_state)}
23 {}
24 stop_token(stop_token const&) = delete;
29
31 {
32 return *shared_state_;
33 }
34
35 private:
36 std::shared_ptr<std::atomic<bool>> shared_state_;
37 };
38
39 // C++20: std::stop_source
41 public:
42 stop_source() = default;
43 stop_source(stop_source const&) = delete;
48
50 {
51 // as-per the C++20 spec, but always true for this impl.
52 const bool has_stop_state = shared_state_ != nullptr;
53 const bool already_stopped = shared_state_->exchange(true);
54
56 }
57
59 {
60 return stop_token{shared_state_};
61 }
62
63 private:
64 std::shared_ptr<std::atomic<bool>> shared_state_ = std::make_shared<std::atomic<bool>>(false);
65 };
66#endif
67}
Definition stop_token.h:40
stop_source(stop_source const &)=delete
stop_token get_token() const noexcept
Definition stop_token.h:58
stop_source(stop_source &&) noexcept=default
bool request_stop() noexcept
Definition stop_token.h:49
Definition stop_token.h:19
bool stop_requested() const noexcept
Definition stop_token.h:30
stop_token(stop_token const &)=delete
stop_token(stop_token &&) noexcept=default
stop_token(std::shared_ptr< std::atomic< bool > > shared_state)
Definition stop_token.h:21
Definition stop_token.h:14
constexpr U to(T &&value)
Definition conversion.h:56
and
Definition algorithms.h:158