Skip to content

Instantly share code, notes, and snippets.

@e7
Last active January 11, 2018 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save e7/8b6238edea4f43e4cf3505aec85f0c9c to your computer and use it in GitHub Desktop.
Save e7/8b6238edea4f43e4cf3505aec85f0c9c to your computer and use it in GitHub Desktop.
raii确保c++11线程被join
class scoped_thread
{
std::thread t;
public:
explicit scoped_thread(std::thread t_) : t(std::move(t_)) {
if (!t.joinable()) {
throw std::logic_error(“No thread”);
}
}
~scoped_thread() {
t.join();
}
scoped_thread(scoped_thread const &) = delete;
scoped_thread &operator=(scoped_thread const &) = delete;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment