Skip to content
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

Fix for: issue #471 Adds gsl::joining_thread (replaces original PR #509) #596

Closed
wants to merge 7 commits into from

Conversation

galik
Copy link
Contributor

@galik galik commented Dec 5, 2017

This PR adds <gsl/gsl_thread> and tests/thread_tests.cpp

As per discussion in CppCoreGuidelines isocpp/CppCoreGuidelines#925

This PR provides gsl::joining_thread (gsl::detached_thread was removed)

(This PR is a cleaned up version of PR #509 - deleted)

joining_thread() noexcept = default;

joining_thread(joining_thread const&) = delete;
joining_thread(joining_thread&& other) : t(std::move(other.t)) {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing forbids you to call joinable() on a moved from std::thread object.

@MikeGitb
Copy link
Contributor

MikeGitb commented Dec 5, 2017

I have to say, I'd feel much more comfortable with the idea of joining threads, if there was a way to indicate to the thread that termination was requested (something like a gsl:: joining_thread::request_stop()/ gsl::this_thread::stop_requested() pair, but that comes with overhead and is probably out of scope for the gsl.

joining_thread() noexcept = default;

joining_thread(joining_thread const&) = delete;
joining_thread(joining_thread&& other) : t(std::move(other.t)) {}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if this PR is still active, but: this move constructor should be noexcept, or even better, defaulted.

joining_thread(std::thread&& other) noexcept : t(std::move(other)) {}

joining_thread& operator=(joining_thread const&) = delete;
joining_thread& operator=(joining_thread&& other) noexcept

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be =defaulted.

return *this;
}

joining_thread& operator=(std::thread const&) = delete;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function signature should not exist (it shouldn't be explicitly =deleted).

joining_thread(joining_thread&& other) : t(std::move(other.t)) {}

joining_thread(std::thread const&) = delete;
joining_thread(std::thread&& other) noexcept : t(std::move(other)) {}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if we're implementing a formal spec here, but if the design is still up for grabs, I'd be strongly inclined to make this constructor explicit — for the same reason that explicit unique_ptr<T>(T *p) is explicit.

If you do make it explicit, then you should remove the signature joining_thread& operator=(std::thread&& other) noexcept below, because we wouldn't want implicit assignment to work either, in that case.


~joining_thread() { if (t.joinable()) t.join(); }

bool joinable() const { return t.joinable(); }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noexcept?

void swap(joining_thread& t1, joining_thread& t2) noexcept
{
using std::swap;
swap(t1.t, t2.t);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to keep this as a hidden friend (defined in-line in the class body). Also, the implementation could be just t1.swap(t2); for simplicity.

@galik galik closed this Jun 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants