gpt4 book ai didi

c++ - 在自定义类中包装 std::packaged_task

转载 作者:太空狗 更新时间:2023-10-29 21:25:13 26 4
gpt4 key购买 nike

我试图将 std::packaged_task 包装在另一个类中,以便与任务调度程序一起使用。

目前,除了 std::future 支持外,我的一切都正常工作。为了获得 std::future 支持,我发现我需要将 std::packaged_task 用于它提供的 get_future() 函数。

我一整天都在尝试各种方法来让它工作,但我似乎无法使用 std::bind 的返回值正确声明和初始化 packaged_task。我试图破译所有相关 libstdc++ 函数的实现,例如 std::async、std::future、std::thread 等,但没有成功。

以下代码是非工作版本和工作版本的实现。要使其正常工作,请取消注释两个/* --- WORKS*/并注释其他相关行。

#include <vector>
#include <deque>
#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
#include <iostream>
#include <chrono>
#include <functional>

#include <windows.h>

class task
{
private:
struct task_implementation_base
{
virtual void execute() = 0;
};

template <class callable>
struct task_implementation : public task_implementation_base
{
task_implementation(callable&& f) : /*m_task(std::forward<callable>(f)) WORKS*/m_task(f) { }
void execute() { m_task(); }

//callable m_task; // WORKS
std::packaged_task<typename result_of<callable>::type> m_task;
};

template <class callable>
std::shared_ptr<task_implementation<callable>> make_routine(callable&& f)
{
return std::make_shared<task_implementation<callable>>(std::forward<callable>(f));
}

public:
template <class callable, class... arguments>
task(callable&& f, arguments&&... args) : m_function(make_routine(std::bind(std::forward<callable>(f), std::forward<arguments>(args)...))) {}

void operator()() { run(); }

void run() { m_function->execute(); }

private:
std::shared_ptr<task_implementation_base> m_function;

};

int testint(int i)
{
std::cout << "test6" << " :: ran from thread " << std::this_thread::get_id() << "\n";
fflush(stdout);
return i+100;
}

void test(const char* text)
{
std::cout << text << " :: ran from thread " << std::this_thread::get_id() << "\n";
fflush(stdout);
}

class testclass
{
public:
void print1() { test("test3"); }
void print2() { test("test4"); }
void print3(const char* text) { test(text); }

};

int main()
{
testclass testclass1;
testclass* testclass2 = new testclass;

task test1(test, "test1");
task test2([]() { test("test2"); });
task test3(&testclass::print1, &testclass1);
task test4(&testclass::print2, &*testclass2);
task test5(&testclass::print3, &*testclass2, "test5");
task test6(&testint, 1);

test1();
test2();
test3();
test4();
test5();
test6();

Sleep(2000);

return 0;
}

我认为问题是 typename result_of<callable>::type .我猜它没有正确评估 callable 的返回类型功能。

我正在使用 c++ (Built by MinGW-builds project) 4.8.0 20121225 (experimental)Windows 8 64bit 上.我怀疑这些错误是无关紧要的,因为我想我只是想以错误的方式进行这项工作,但这里有一个错误的 pastebin:errors

最佳答案

std::packaged_task 不仅将被调用函数的结果类型作为模板参数,而且还将您传递给被调用函数的参数类型作为模板参数。

您可以按如下方式定义它们:

// somewhere
int foo(bool, int);

// somewhere else
std::packaged_task<int(bool, int)> p(foo);

要修复您的代码,您需要添加两个空括号对。我上面解释的内容也适用于 std::result_of

std::packaged_task<typename std::result_of<callable()>::type()> m_task;

关于c++ - 在自定义类中包装 std::packaged_task,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14346152/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com