设置packaged_task的正确方法

时间:2015-02-21 00:58:50

标签: c++ multithreading c++11 most-vexing-parse packaged-task

编译下面的程序时,我收到错误消息:

错误1错误C2228:'。get_future'左边必须有class / struct / union c:\ users \ haliaga \ documents \ visual studio 2010 \ projects \ test \ test \ accumulateexceptionsafe.cpp 62 1测试< / em>的

这实际上并不是真正的问题。

如果你评论这些行:

//futures[i]=task.get_future();
//threads[i]=std::thread(std::move(task),block_start,block_end);
//block_start=block_end;

你会收到下面的警告,说没有叫“tasK”:

*警告C4930:'std :: packaged_task&lt;&gt; task(accumulate_block(__ cdecl )(void))':未调用prototyped函数(是一个变量定义?) 1 GT;同 1 GT; [ 1 GT; = int(std :: _ List_iterator&gt;&gt;,std :: _ List_iterator&gt;&gt;), 1 GT;迭代=标准:: _ List_iterator&GT;&gt;中 1 GT; T = INT 1 GT; ]

指定的正确方法是什么:

std::packaged_task<T(Iterator,Iterator)> task(accumulate_block<Iterator,T>());

谢谢

PS:找到下面的代码:

#include <list>
#include <numeric>
#include <vector>
#include <thread>
#include <future>

using namespace std;
template<typename Iterator,typename T>
struct accumulate_block
{
    T operator()(Iterator first, Iterator last)
    {
        std::thread::id id = std::this_thread::get_id();
        return std::accumulate(first, last, T());
    }
};

class join_threads
{
    std::vector<std::thread>& threads;
public:
    explicit join_threads(std::vector<std::thread>& threads_):
    threads(threads_)
    {
        std::thread::id id = std::this_thread::get_id();
    }
    ~join_threads()
    {
        std::thread::id id = std::this_thread::get_id();
        for(unsigned long i=0;i<threads.size();++i)
        {
            if(threads[i].joinable())
                threads[i].join();
        }
    }
};

template<typename Iterator,typename T>
T parallel_accumulate(Iterator first,Iterator last,T init)
{
    std::thread::id id = std::this_thread::get_id();
    unsigned long const length=std::distance(first,last);
    if(!length)
        return init;
    unsigned long const min_per_thread=25;
    unsigned long const max_threads=(length+min_per_thread-1)/min_per_thread;
    unsigned long const hardware_threads=std::thread::hardware_concurrency();
    unsigned long const num_threads=std::min(hardware_threads!=0?hardware_threads:2,max_threads);
    unsigned long const block_size=length/num_threads;
    std::vector<std::future<T> > futures(num_threads-1);
    std::vector<std::thread> threads(num_threads-1);
    join_threads joiner(threads);
    Iterator block_start=first;
    for(unsigned long i=0;i<(num_threads-1);++i)
    {
        Iterator block_end=block_start;
        std::advance(block_end,block_size);
        std::packaged_task<T(Iterator,Iterator)> task(accumulate_block<Iterator,T>());
        futures[i]=task.get_future();
        threads[i]=std::thread(std::move(task),block_start,block_end);
        block_start=block_end;
    }
    T last_result=accumulate_block<Iterator, T>()(block_start,last);
    T result=init;
    for(unsigned long i=0;i<(num_threads-1);++i)
    {
        result+=futures[i].get();
    }
    result += last_result;
    return result;
};

int main()
{
    list<int> l;
    for(int i=0; i<26; ++i)
        l.push_back(i);

    std::thread::id id = std::this_thread::get_id();
    int res = ::parallel_accumulate(l.begin(), l.end(), 0);

    return 0;
}

1 个答案:

答案 0 :(得分:4)

最令人烦恼的解析。

std::packaged_task<T(Iterator,Iterator)> task(accumulate_block<Iterator,T>());

声明一个名为task的函数,它接受一个类型指针的参数,该函数不带参数并返回accumulate_block<Iterator,T>并返回std::packaged_task<T(Iterator,Iterator)>

使用统一初始化语法消除歧义:

std::packaged_task<T(Iterator,Iterator)> task(accumulate_block<Iterator,T>{});

或者对于不支持统一初始化的古代编译器使用额外的括号:

std::packaged_task<T(Iterator,Iterator)> task((accumulate_block<Iterator,T>()));
//                                            ^                              ^