通用 C++11 函数包装器,用于基于任务的并行性

generic c++11 function wrapper for task based parallelism

本文关键字:任务 并行性 于任务 函数 C++11 包装 用于 通用      更新时间:2023-10-16

>我正在实现一个工作窃取算法,并且正在编写一个通用函数包装器,该包装器将承诺作为包装器模板的可变参数之一。我想使用这些函数包装器创建任务,并让每个节点使用 promise 与依赖节点进行通信。每个节点维护一个依赖节点和承诺/期货的列表。每个节点都可以通过检查是否设置了所有期货来运行。承诺可能会有所不同,具体取决于函数包装器返回不同对象的作业。如果单个算法可以分解为单独的操作,如读取消息和解码消息,对对象执行检查,返回所有检查的结果,则这些操作中的每一个都将返回不同的承诺(对象,布尔值,结果)。

这本名为《C++并发在行动》的书有一个函数包装器实现,但它不能处理这个用例。在网上的其他参考资料中,我看到了对 std::p romise 等承诺的硬编码引用,这只是一种类型。

有人可以建议我如何编写包装器来实现以下目标吗?

void add(int a, int b, std::promise<int>&& prms)
{
int res = a + b;
try {
prms.set_value(res);
}
catch(...)
{
prms.set_exception(std::current_exception());
}
}
int main()
{
std::promise<int> prms;
std::future<int> fut = prms.get_future();
FunctionWrapper myFunctor(a, 10, 20, std::move(prms));
// add the functor to the queue and it will be retrieved by a thread
// that executes the task. since i have the future, i can pass it to the 
// dependent worknode
}

我尝试编写如下代码...但在使其工作时面临困难。

#ifndef FUNCTIONWRAPPER_HPP
#define FUNCTIONWRAPPER_HPP
template<typename F, typename R, typename... Args>
class FunctionWrapper
{
class implbase
{
public:
virtual ~implbase();
virtual R execute(Args...)=0;
};
class impl : public implbase
{
public:
impl(F&& f) : func(std::move(f)) {}
virtual R execute(Args... args) { return func(args...); }
private:
F func;
};
std::shared_ptr<impl> internalFunc;
public:
FunctionWrapper(F&& f) : internalFunc(0)
{
internalFunc = new impl<F, R, Args...>(f);
}
FunctionWrapper(const FunctionWrapper& other)
: internalFunc(std::move(other.internalFunc))
{}
~FunctionWrapper()
{
if(internalFunc)
delete internalFunc;
}
R operator()(Args... args)
{
return internalFunc->execute(args...);
}
void swap(FunctionWrapper& other)
{
impl<R, Args...>* tmp = internalFunc;
internalFunc = other.internalFunc;
other.internalFunc = tmp;
}
FunctionWrapper& operator=(const FunctionWrapper& other)
{
FunctionWrapper(other).swap(*this);
return *this;
}
FunctionWrapper& operator=(const F& f)
{
FunctionWrapper(f).swap(*this);
return *this;
}
};
#endif // FUNCTIONWRAPPER_HPP

C++11 有一个包装器来做到这一点!它被称为packaged_task

它的作用是包装一个可调用的对象(函数对象、lambda、函数指针、绑定表达式等),并通过与传入函数的返回类型匹配的get_future()方法为您提供未来。

请考虑以下示例:

#include <thread>
#include <future>
#include <functional>
#include <iostream>
using namespace std;
int add(int a, int b)
{
return a + b;
}
int main()
{
// Create a std::packaged_task and grab the future out of it.
packaged_task<int()> myTask(bind(add, 10, 20));
future<int> myFuture = myTask.get_future();
// Here, is where you would queue up the task in your example.
// I'll launch it on another thread just to demonstrate how.
thread myThread(std::move(myTask));
myThread.detach();
// myFuture.get() will block until the task completes.
// ...or throw if the task throws an exception.
cout << "The result is: " << myFuture.get() << endl;
return 0;
}

正如你所看到的,我们指望packaged_task创造承诺并给我们未来,而不是在promise中通过

。此外,使用 bind 表达式允许我们有效地将参数交给任务以保留,直到它被调用。

使用packaged_task还将推动未来异常的负担交给packaged_task。这样,您的函数就不需要调用set_exception()。他们只需要返回或扔掉。