- void start() // noexcept\r
- {\r
- if(is_running_.fetch_and_store(true))\r
- return;\r
- thread_ = boost::thread(f_);\r
+}\r
+\r
+enum task_priority\r
+{\r
+ high_priority,\r
+ normal_priority,\r
+ priority_count\r
+};\r
+\r
+enum thread_priority\r
+{\r
+ high_priority_class,\r
+ above_normal_priority_class,\r
+ normal_priority_class,\r
+ below_normal_priority_class\r
+};\r
+\r
+class executor : boost::noncopyable\r
+{\r
+ const std::string name_;\r
+ boost::thread thread_;\r
+ tbb::atomic<bool> is_running_;\r
+ \r
+ typedef tbb::concurrent_bounded_queue<std::function<void()>> function_queue;\r
+ function_queue execution_queue_[priority_count];\r
+ \r
+ template<typename Func>\r
+ auto create_task(Func&& func) -> boost::packaged_task<decltype(func())> // noexcept\r
+ { \r
+ typedef boost::packaged_task<decltype(func())> task_type;\r
+ \r
+ auto task = task_type(std::forward<Func>(func));\r
+ \r
+ task.set_wait_callback(std::function<void(task_type&)>([=](task_type& my_task) // The std::function wrapper is required in order to add ::result_type to functor class.\r
+ {\r
+ try\r
+ {\r
+ if(boost::this_thread::get_id() == thread_.get_id()) // Avoids potential deadlock.\r
+ my_task();\r
+ }\r
+ catch(boost::task_already_started&){}\r
+ }));\r
+ \r
+ return std::move(task);\r