Created
April 6, 2017 14:05
-
-
Save ivcn/65f8fc5b75d04cda5476c16ab10b81fd to your computer and use it in GitHub Desktop.
Wrapper for std::thread. Provides automatic join of owned thread.
This file contains hidden or 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 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