-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtask.hpp
57 lines (42 loc) · 960 Bytes
/
task.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef __TASK_H__
#define __TASK_H__
#include "abstract_task.hpp"
#include "scheduler.hpp"
#include <functional>
#include <future>
namespace cosche
{
template<class Rt>
class Task : public AbstractTask
{
public:
Task(Scheduler& scheduler) :
AbstractTask(scheduler),
_task(std::make_shared<std::packaged_task<Rt()>>())
{}
template <class Fn, class... Args>
void operator()(Fn&& fn, Args&&... args)
{
*_task = std::packaged_task<Rt()>(std::bind(fn, args...));
}
void run()
{
if (_task->valid())
{
_task->operator()();
}
}
std::size_t id() const
{
return reinterpret_cast<std::size_t>(_task.get());
}
void recycle()
{
scheduler().recycle(this);
}
auto getFuture() { return _task->get_future(); }
private:
std::shared_ptr<std::packaged_task<Rt()>> _task;
};
} // end cosche namespace
#endif // __TASK_H__