I am trying to write a wrapper for std::thread with a run method , which will allow thread to execute only once run is called.
class ThreadRAII_WITHRUN {
public:
enum class DtorAction { join, detach };
template< class Function, class... Args >
ThreadRAII_WITHRUN(DtorAction a,Function&& f, Args&&... args)
: action(a)
, t(std::thread([&](){pro.get_future().wait(); std::forward<Function>(f)(std::forward<Args>(args)...);}))
{
}
void run()
{
pro.set_value();
}
~ThreadRAII_WITHRUN()
{
if (t.joinable()) { // joinability test
if (action == DtorAction::join) {
t.join();
} else {
t.detach();
}
}
}
ThreadRAII_WITHRUN(ThreadRAII_WITHRUN&&) = default; // support
ThreadRAII_WITHRUN& operator=(ThreadRAII_WITHRUN&&) = default; // moving
std::thread& get() { return t; }
private: // as before
DtorAction action;
std::promise<void> pro;
std::thread t;
};
This code compiles with gcc6.1.0 but not with gcc4.8.5
does anyboday know the reason for the same?
Aucun commentaire:
Enregistrer un commentaire