c ++:错误:在'class std :: result_of< void(*(std :: unordered_map [英] c++: error: no type named ‘type’ in ‘class std::result_of<void (*(std::unordered_map

查看:124
本文介绍了c ++:错误:在'class std :: result_of< void(*(std :: unordered_map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下只是使用两个线程插入哈希表进行测试的简单程序.对于测试,不使用锁.

Following is just a simple program to test using two threads to insert a hash table. For test no lock is used.

#include <iostream>
#include <unordered_map>
#include <thread>

using namespace std;

void thread_add(unordered_map<int, int>& ht, int from, int to)
{
    for(int i = from; i <= to; ++i)
        ht.insert(unordered_map<int, int>::value_type(i, 0));
}

void test()
{
    unordered_map<int, int> ht;
    thread t[2];

    t[0] = thread(thread_add, ht, 0, 9);
    t[1] = thread(thread_add, ht, 10, 19);

    t[0].join();
    t[1].join();

    std::cout << "size: " << ht.size() << std::endl;
}

int main()
{
    test();
    return 0;
}

但是,编译时会出错.

$ g++ -std=c++11 -pthread test.cpp
...
/usr/include/c++/4.8.2/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<void (*(std::unordered_map<int, int>, int, int))(std::unordered_map<int, int>&, int, int)>’
       typedef typename result_of<_Callable(_Args...)>::type result_type;
...

花点时间,但仍然无法纠正它.谢谢.

Took a while but still cannot correct it. Thanks.

推荐答案

我可以使用MSVC2013成功编译您的代码.但是,thread()可以将其参数的副本传递给新线程.这意味着,如果您的代码可以在编译器上编译,则每个线程wourd都将使用其自己的ht副本运行,因此最后,mainht将为空.

I could compile your code successfully with MSVC2013. However, thread() works passing copies of its argument to the new thread. This means that if your code would compile on your compiler, each thread wourd run with its own copy of ht, so that at the end, main's ht would be empty.

GCC无法使用此奇怪的消息进行编译.您可以通过将引用包装器与线程一起使用来消除它:

GCC doesn't compile with this weird message. You can get rid of it by using the reference wraper with thread:

t[0] = thread(thread_add, std::ref(ht), 0, 9);
t[1] = thread(thread_add, std::ref(ht), 10, 19);

这将成功编译.并且线程使用的每个引用将引用相同的对象.

This will compile succesfully. And each reference used by the threads would refer to the same object.

但是,您很可能会遇到一些运行时错误或意外结果.这是因为两个线程正在同时尝试插入ht中.但是unordered_map并不是线程安全的,因此这些竞争条件可能导致ht达到不稳定状态(即UB,即潜在的段错误).

However, there are high chances that you'll get some runtime error or unexpected results. This is because two threads are concurently trying to insert into ht. But unordered_map is not thread safe, so these racing conditions might cause ht to reach an unstable state (i.e. UB, i.e. potential segfault).

要使其正常运行,必须保护并发访问:

To make it running properly, you have to protect your concurent accesses:

#include <mutex>
...
mutex mtx;   // to protect against concurent access

void thread_add(unordered_map<int, int>& ht, int from, int to)
{
    for (int i = from; i <= to; ++i) {
        std::lock_guard<std::mutex> lck(mtx);  // protect statements until end of block agains concurent access
        ht.insert(unordered_map<int, int>::value_type(i, 0));
    }
}

这篇关于c ++:错误:在'class std :: result_of&lt; void(*(std :: unordered_map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆