Skip to content

Instantly share code, notes, and snippets.

@ivcn
Created April 6, 2017 14:05
Show Gist options
  • Select an option

  • Save ivcn/65f8fc5b75d04cda5476c16ab10b81fd to your computer and use it in GitHub Desktop.

Select an option

Save ivcn/65f8fc5b75d04cda5476c16ab10b81fd to your computer and use it in GitHub Desktop.
Wrapper for std::thread. Provides automatic join of owned thread.
class thread_handle {
private:
std::thread t;
int id;
public:
template<typename F, typename... Args>
thread_handle(int _id, F&& f, Args&&... args) :
id(_id),
t(std::move(std::thread(f, args...))) {
}
thread_handle() : id(0) { }
thread_handle(const thread_handle& other) = delete;
thread_handle& operator=(const thread_handle& other) = delete;
thread_handle(thread_handle&& other) :
t(std::move(other.t)),
id(other.id) {
}
thread_handle& operator=(thread_handle&& other) { t = std::move(other.t); }
~thread_handle() {
if(t.joinable())
t.join();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment