Tom*_*Tom 3 c++ multithreading future threadpool
我正在开发一个需要在线程池中运行许多作业的C++项目.这些作业容易出错,这意味着我需要知道每个作业在完成后如何终止.作为Java程序员,我喜欢使用"期货"或类似范例的想法,类似于Java的util.concurrent包中的各种类.
我有两个问题:首先,C++已经存在这样的事情(我在Boost中找不到任何东西,但也许我看起来不够努力); 第二,这对C++来说是一个理智的想法吗?
我找到了一个简短的例子,说明我在这里要完成的任务:
http://www.boostcookbook.com/Recipe:/1234841
这种方法有意义吗?
Boost 实施了期货和其他线程工具.
请注意,当您调用get()方法时boost::unique_future,它将重新抛出异步执行期间可能已存储在其中的任何异常.
我建议你这样做:
#pragma once
#include <tbb/concurrent_queue.h>
#include <boost/thread.hpp>
#include <boost/noncopyable.hpp>
#include <functional>
namespace internal
{
template<typename T>
struct move_on_copy
{
move_on_copy(const move_on_copy<T>& other) : value(std::move(other.value)){}
move_on_copy(T&& value) : value(std::move(value)){}
mutable T value;
};
template<typename T>
move_on_copy<T> make_move_on_copy(T&& value)
{
return move_on_copy<T>(std::move(value));
}
}
class executor : boost::noncopyable
{
boost::thread thread_;
tbb::concurrent_bounded_queue<std::function<void()>> execution_queue_;
template<typename Func>
auto create_task(Func&& func) -> boost::packaged_task<decltype(func())> // noexcept
{
typedef boost::packaged_task<decltype(func())> task_type;
auto task = task_type(std::forward<Func>(func));
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.
{
try
{
if(boost::this_thread::get_id() == thread_.get_id()) // Avoids potential deadlock.
my_task();
}
catch(boost::task_already_started&){}
}));
return std::move(task);
}
public:
explicit executor() // noexcept
{
thread_ = boost::thread([this]{run();});
}
~executor() // noexcept
{
execution_queue_.push(nullptr); // Wake the execution thread.
thread_.join();
}
template<typename Func>
auto begin_invoke(Func&& func) -> boost::unique_future<decltype(func())> // noexcept
{
// Create a move on copy adaptor to avoid copying the functor into the queue, tbb::concurrent_queue does not support move semantics.
auto task_adaptor = internal::make_move_on_copy(create_task(func));
auto future = task_adaptor.value.get_future();
execution_queue_.push([=]
{
try{task_adaptor.value();}
catch(boost::task_already_started&){}
});
return std::move(future);
}
template<typename Func>
auto invoke(Func&& func) -> decltype(func()) // noexcept
{
if(boost::this_thread::get_id() == thread_.get_id()) // Avoids potential deadlock.
return func();
return begin_invoke(std::forward<Func>(func), prioriy).get();
}
private:
void run() // noexcept
{
while(true)
{
std::function<void()> func;
execution_queue_.pop(func);
if(!func)
break;
func();
}
}
};
Run Code Online (Sandbox Code Playgroud)
期货都出现在即将推出的标准(C++ 0x)和内部提升中.请注意,虽然主名称future相同,但您需要阅读文档以查找其他类型并理解语义.我不知道Java期货,所以我不能告诉你他们的不同之处,如果有的话.
boost中的库是由Anthony Williams编写的,我相信我也参与了该部分标准的定义.他还编写了C++ Concurrency in Action,其中包括对期货,任务,承诺和相关对象的良好描述.如果您有兴趣,他的公司还会销售完整的C++ 0x线程库实现.
| 归档时间: |
|
| 查看次数: |
2322 次 |
| 最近记录: |