TimeOutTask.h
// TimeOutTask.h
//
// Author David Barrett-Lennard
// (C)opyright Cedanet Pty Ltd 2013
#pragma once
#ifndef Ceda_cxMessage_TimeOutTask_H
#define Ceda_cxMessage_TimeOutTask_H
#include "cxMessage.h"
#include "AutoResetEvent.h"
#include "ThreadName.h"
#include <thread>
#include <functional>
#include <atomic>
#ifdef _MSC_VER
// struct 'X' needs to have dll-interface to be used by clients of class 'Y'
#pragma warning(disable:4251)
#endif
namespace ceda
{
class cxMessage_API TimeOutTask
{
public:
TimeOutTask();
~TimeOutTask();
void Start(const char* threadName, int32 timeOutmsec, std::function<void(bool signaled)> task);
void Stop();
void SetTimeOut(int32 timeOutmsec);
void Signal();
// A task can call this to poll whether it should complete as soon as possible
bool Abort() const { return !started_; }
private:
xstring threadName_;
std::thread thread_;
std::atomic<int32> timeOutmsec_;
volatile bool started_;
AutoResetEvent wakeThreadEvent_;
std::function<void(bool signaled)> task_;
};
} // namespace ceda
#endif // include guard