Created
May 19, 2015 07:57
-
-
Save swkwon/4fe5ad02f012d5117f86 to your computer and use it in GitHub Desktop.
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
#pragma once | |
#include <thread> | |
#include <future> | |
#include <iostream> | |
#include <functional> | |
class SuspendThread { | |
private: | |
std::thread task; | |
std::promise<void> Psuspend; | |
public: | |
template<typename FN_, typename ... Arguments> | |
SuspendThread(FN_ func, Arguments&& ... args) | |
{ | |
std::function<void()> b = std::bind(func, std::forward<Arguments>(args)...); | |
auto l = [this](std::function<void()> f) | |
{ | |
auto fut = this->Psuspend.get_future(); | |
fut.wait(); | |
f(); | |
}; | |
std::thread it(l, b); | |
task.swap(it); | |
} | |
~SuspendThread() | |
{ | |
task.join(); | |
} | |
void run() | |
{ | |
Psuspend.set_value(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment