-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feature/spaceship: Clause 32: Thread support #1590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| #include <tuple> | ||
| #include <xthreads.h> | ||
| #if _HAS_CXX20 | ||
| #include <compare> | ||
| #include <stop_token> | ||
| #endif // _HAS_CXX20 | ||
|
|
||
|
|
@@ -215,7 +216,11 @@ private: | |
| friend thread::id thread::get_id() const noexcept; | ||
| friend thread::id this_thread::get_id() noexcept; | ||
| friend bool operator==(thread::id _Left, thread::id _Right) noexcept; | ||
| #if _HAS_CXX20 | ||
| friend strong_ordering operator<=>(thread::id _Left, thread::id _Right) noexcept; | ||
| #else // ^^^ _HAS_CXX20 / !_HAS_CXX20 vvv | ||
| friend bool operator<(thread::id _Left, thread::id _Right) noexcept; | ||
| #endif // !_HAS_CXX20 | ||
| template <class _Ch, class _Tr> | ||
| friend basic_ostream<_Ch, _Tr>& operator<<(basic_ostream<_Ch, _Tr>& _Str, thread::id _Id); | ||
| friend hash<thread::id>; | ||
|
|
@@ -237,6 +242,11 @@ _NODISCARD inline bool operator==(thread::id _Left, thread::id _Right) noexcept | |
| return _Left._Id == _Right._Id; | ||
| } | ||
|
|
||
| #if _HAS_CXX20 | ||
| _NODISCARD inline strong_ordering operator<=>(thread::id _Left, thread::id _Right) noexcept { | ||
| return _Left._Id <=> _Right._Id; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fun fact: the Standard doesn't require the total ordering over thread ids that this function observes to be consistent with (I've submitted an LWG issue to fix this.) |
||
| } | ||
| #else // ^^^ _HAS_CXX20 / !_HAS_CXX20 vvv | ||
| _NODISCARD inline bool operator!=(thread::id _Left, thread::id _Right) noexcept { | ||
| return !(_Left == _Right); | ||
| } | ||
|
|
@@ -256,6 +266,7 @@ _NODISCARD inline bool operator>(thread::id _Left, thread::id _Right) noexcept { | |
| _NODISCARD inline bool operator>=(thread::id _Left, thread::id _Right) noexcept { | ||
| return !(_Left < _Right); | ||
| } | ||
| #endif // !_HAS_CXX20 | ||
|
|
||
| template <class _Ch, class _Tr> | ||
| basic_ostream<_Ch, _Tr>& operator<<(basic_ostream<_Ch, _Tr>& _Str, thread::id _Id) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
| #include <stack> | ||
| #include <string> | ||
| #include <system_error> | ||
| #include <thread> | ||
| #include <type_traits> | ||
| #include <unordered_map> | ||
| #include <unordered_set> | ||
|
|
@@ -481,6 +482,14 @@ void ordering_test_cases() { | |
|
|
||
| spaceship_test<std::strong_ordering>(c_mem[0], c_mem[0], c_mem[1]); | ||
| } | ||
| { // thread::id | ||
| std::thread::id id1; | ||
| std::thread::id id1_equal; | ||
| std::thread::id id2 = std::this_thread::get_id(); | ||
|
|
||
| // Implementation-specific assumption: std::thread::id{} occurs first in the unspecified total ordering. | ||
| spaceship_test<std::strong_ordering>(id1, id1_equal, id2); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe there's an implementation assumption here - nothing in the Standard appears to require that |
||
| } | ||
| } | ||
|
|
||
| template <class Element, class Ordering> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to default the
operator==for C++20 and make them hidden friends?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They can't be hidden friends; the difference is observable. Arguably
operator==also cannot be defaulted because doing so would make itconstexprand we're forbidden to make thingsconstexprwhere the Standard doesn't depict them as such.