tenno 0.1.0
Loading...
Searching...
No Matches
thread.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Author: Giovanni Santini
3// Mail: giovanni.santini@proton.me
4// Github: @San7o
5
6#pragma once
7
8#include <thread> // based on thread
9
10namespace tenno
11{
12
18{
19public:
23 using native_handle_type = std::thread::native_handle_type;
24
25 std::thread::id id;
26
27 jthread() noexcept = default;
28 jthread(const jthread &) = delete;
29
35 jthread(jthread &&other) noexcept
36 {
37 this->_inner_thread = std::move(other._inner_thread);
38 this->id = this->_inner_thread.get_id();
39 }
40
49 template <class F, class... Args> explicit jthread(F &&f, Args &&...args)
50 {
51 this->_inner_thread = std::thread(f, args...);
52 this->id = this->_inner_thread.get_id();
53 }
54
56 {
57 if (this->joinable())
58 {
59 this->_inner_thread.join();
60 }
61 }
62
63 bool joinable() const noexcept
64 {
65 return this->_inner_thread.joinable();
66 }
67
68 std::thread::id get_id() const noexcept
69 {
70 return this->_inner_thread.get_id();
71 }
72
79 {
80 return this->_inner_thread.native_handle();
81 }
82
90 static unsigned int hardware_concurrency() noexcept
91 {
92 return std::thread::hardware_concurrency();
93 }
94
95 void join()
96 {
97 this->_inner_thread.join();
98 }
99
100 void detach()
101 {
102 this->_inner_thread.detach();
103 }
104
105 void swap(tenno::jthread &other) noexcept
106 {
107 this->_inner_thread.swap(other._inner_thread);
108 this->id = this->_inner_thread.get_id();
109 other.id = other._inner_thread.get_id();
110 }
111
112#if __cplusplus >= 202002L // C++20
113 std::stop_source get_stop_source() noexcept
114 {
115 return this->_stop_source;
116 }
117
118 std::stop_token get_stop_token() const noexcept
119 {
120 return this->_stop_source.get_token();
121 }
122
123 bool request_stop() noexcept
124 {
125 return this->_stop_source.request_stop();
126 }
127#endif
128
129private:
130 std::thread _inner_thread;
131#if __cplusplus >= 202002L // C++20
132 std::stop_source _stop_source;
133#endif
134};
135
136} // namespace tenno
The jthread class is a wrapper around std::thread that automatically rejoins the thread on destructio...
Definition thread.hpp:18
jthread(F &&f, Args &&...args)
Construct a new jthread object with the given function and arguments.
Definition thread.hpp:49
bool joinable() const noexcept
Definition thread.hpp:63
jthread() noexcept=default
static unsigned int hardware_concurrency() noexcept
Get the number of concurrent threads supported by the implementation.
Definition thread.hpp:90
std::thread::id id
Definition thread.hpp:25
native_handle_type native_handle()
Get the native handle of the thread.
Definition thread.hpp:78
void swap(tenno::jthread &other) noexcept
Definition thread.hpp:105
std::thread::native_handle_type native_handle_type
The type of the implementation defined thread handle.
Definition thread.hpp:23
void join()
Definition thread.hpp:95
std::thread::id get_id() const noexcept
Definition thread.hpp:68
void detach()
Definition thread.hpp:100