Skip to content

PR for Issue #471 Added Thread Classes: gsl::raii_thread gsl::detached_thread - #481

Closed
Galik (galik) wants to merge 7 commits into
microsoft:masterfrom
galik:thread_classes
Closed

Galik (galik) wants to merge 7 commits into
microsoft:masterfrom
galik:thread_classes

Conversation

@galik

Copy link
Copy Markdown
Contributor

This PR adds <gsl/gsl_thread> and tests/thread_tests.cpp
Fixes for Issue #471

Notes:
I have copied the interface of std::thread except for std::thread::detach. The reason being that gsl::detached_thread is already detached and gsl::raii_thread should not be detached by design.

@galik Galik (galik) changed the title Thread classes PR for Issue #471 Added Thread Classes: gsl::raii_thread gsl::detached_thread Apr 4, 2017
Comment thread include/gsl/gsl_thread

std::thread::native_handle_type native_handle() { return t.native_handle(); }

void join() { t.join(); }

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If the std::thread is actually detached, this will throw. I don't think there's a point in exposing a method that will always just throw no matter what.

Comment thread include/gsl/gsl_thread
raii_thread& operator=(raii_thread const&) = delete;
raii_thread& operator=(raii_thread&& other) noexcept
{
t = std::move(other.t);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This std::terminates if this->t hasn't been joined. This isn't in the spirit of an RAII wrapper. It should likely first properly "reset" this and then move the rhs into it, similarly to what other RAII managers, like unique_ptr would do.

A check if (this != &rhs) on top of it would be needed too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

When you say "reset" this do you mean perform a join()? Because I am not entirely sure about that.

I could be wrong but my feeling is that the main use for this function is when you are moving into a default initialized raii_thread. For example when returning a raii_thread from a function or accepting one as a parameter.

If the client actually assigns a raii_thread to an already running one then performing a join might introduce hidden, unexpected serialization delays and I suspect it would rarely be the intention to move a running raii_threadonto another already running one.

Would that not always be an error?

I am willing to be persuaded either way. I think I would generally rather it trigger a terminal error than silently join because (with an raii_thread) I would probably always want to do the join explicitly using the natural scope.

@hsutter

Copy link
Copy Markdown
Contributor

The main thing is to get raii_thread whose destructor unconditionally joins, which is what std::thread should have done. That part looks fine. Then we can recommend using raii_thread everywhere.

I'm less positive about adding a detached_thread... I'm not sure we should be encouraging detached threads.

Suggestion: Could we separate the PR into two so that we can adopt the raii_thread and further discuss the advisability of detached_thread?

@galik

Copy link
Copy Markdown
Contributor Author

See new PR #504

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