Skip to content

Commit 091958d

Browse files
feature/spaceship: Clause 32: Thread support (#1590)
Co-authored-by: Stephan T. Lavavej <[email protected]>
1 parent 0f71986 commit 091958d

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

stl/inc/thread

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <tuple>
1515
#include <xthreads.h>
1616
#if _HAS_CXX20
17+
#include <compare>
1718
#include <stop_token>
1819
#endif // _HAS_CXX20
1920

@@ -215,7 +216,11 @@ private:
215216
friend thread::id thread::get_id() const noexcept;
216217
friend thread::id this_thread::get_id() noexcept;
217218
friend bool operator==(thread::id _Left, thread::id _Right) noexcept;
219+
#if _HAS_CXX20
220+
friend strong_ordering operator<=>(thread::id _Left, thread::id _Right) noexcept;
221+
#else // ^^^ _HAS_CXX20 / !_HAS_CXX20 vvv
218222
friend bool operator<(thread::id _Left, thread::id _Right) noexcept;
223+
#endif // !_HAS_CXX20
219224
template <class _Ch, class _Tr>
220225
friend basic_ostream<_Ch, _Tr>& operator<<(basic_ostream<_Ch, _Tr>& _Str, thread::id _Id);
221226
friend hash<thread::id>;
@@ -237,6 +242,11 @@ _NODISCARD inline bool operator==(thread::id _Left, thread::id _Right) noexcept
237242
return _Left._Id == _Right._Id;
238243
}
239244

245+
#if _HAS_CXX20
246+
_NODISCARD inline strong_ordering operator<=>(thread::id _Left, thread::id _Right) noexcept {
247+
return _Left._Id <=> _Right._Id;
248+
}
249+
#else // ^^^ _HAS_CXX20 / !_HAS_CXX20 vvv
240250
_NODISCARD inline bool operator!=(thread::id _Left, thread::id _Right) noexcept {
241251
return !(_Left == _Right);
242252
}
@@ -256,6 +266,7 @@ _NODISCARD inline bool operator>(thread::id _Left, thread::id _Right) noexcept {
256266
_NODISCARD inline bool operator>=(thread::id _Left, thread::id _Right) noexcept {
257267
return !(_Left < _Right);
258268
}
269+
#endif // !_HAS_CXX20
259270

260271
template <class _Ch, class _Tr>
261272
basic_ostream<_Ch, _Tr>& operator<<(basic_ostream<_Ch, _Tr>& _Str, thread::id _Id) {

tests/std/tests/P1614R2_spaceship/test.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <stack>
2222
#include <string>
2323
#include <system_error>
24+
#include <thread>
2425
#include <type_traits>
2526
#include <unordered_map>
2627
#include <unordered_set>
@@ -481,6 +482,14 @@ void ordering_test_cases() {
481482

482483
spaceship_test<std::strong_ordering>(c_mem[0], c_mem[0], c_mem[1]);
483484
}
485+
{ // thread::id
486+
std::thread::id id1;
487+
std::thread::id id1_equal;
488+
std::thread::id id2 = std::this_thread::get_id();
489+
490+
// Implementation-specific assumption: std::thread::id{} occurs first in the unspecified total ordering.
491+
spaceship_test<std::strong_ordering>(id1, id1_equal, id2);
492+
}
484493
}
485494

486495
template <class Element, class Ordering>

0 commit comments

Comments
 (0)