Skip to content

Instantly share code, notes, and snippets.

@swkwon
Created May 19, 2015 07:57
Show Gist options
  • Save swkwon/4fe5ad02f012d5117f86 to your computer and use it in GitHub Desktop.
Save swkwon/4fe5ad02f012d5117f86 to your computer and use it in GitHub Desktop.
#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