Skip to content

Instantly share code, notes, and snippets.

@swkwon
Last active August 29, 2015 14:21
Show Gist options
  • Save swkwon/b23ad4801464e0bfee26 to your computer and use it in GitHub Desktop.
Save swkwon/b23ad4801464e0bfee26 to your computer and use it in GitHub Desktop.
class SuspendThread {
private:
std::thread task;
std::promise<void> Psuspend;
public:
template<typename FN_, typename ... Arguments>
SuspendThread(FN_ func, Arguments&& ... args)
{
auto fut_suspend = Psuspend.get_future();
std::thread it(
[func](std::future<void>&& fut, auto&&... args)
{
fut.wait();
func(std::forward<Arguments>(args)...);
}, std::move(fut_suspend), std::forward<Arguments>(args)...);
task.swap(it);
}
~SuspendThread()
{
task.join();
}
void run() noexcept
{
Psuspend.set_value();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment