Last active
January 11, 2018 14:32
-
-
Save e7/8b6238edea4f43e4cf3505aec85f0c9c to your computer and use it in GitHub Desktop.
raii确保c++11线程被join
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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