请教:c++11 用成员函数创建多线程传递参数的问题

tj807126663 2014-05-29 05:14:45
在调试http://www.cplusplus.com/reference/thread/thread/thread/的示例程序的时候一直报错,如果将调用函数修改成无参数类型的话,程序是可以成功编译并运行的,请朋友们帮我看一下问题在哪里?
精简后的代码如下:
#include <iostream>       // std::cout
#include <atomic> // std::atomic
#include <thread> // std::thread
#include <vector> // std::vector

struct C : std::atomic<int> {
C() : std::atomic<int>(0) {}
void increase_member (int n) { for (int i=0; i<n; ++i) fetch_add(1); }
};

int main ()
{
std::vector<std::thread> threads;

std::cout << "increase counter (bar) with 10 threads using member...\n";
C bar;
for (int i=1; i<=10; ++i)
threads.push_back(std::thread(&C::increase_member,std::ref(bar),1000));

std::cout << "synchronizing all threads...\n";
for (auto& th : threads) th.join();

std::cout << "bar: " << bar << '\n';

return 0;
}

错误提示如下:
In file included from /usr/include/c++/4.8/thread:39:0,
from example.cpp:3:
/usr/include/c++/4.8/functional: In instantiation of ‘struct std::_Bind_simple<std::_Mem_fn<void (C::*)(int)>(std::reference_wrapper<C>, int)>’:
/usr/include/c++/4.8/thread:137:47: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (C::*)(int); _Args = {std::reference_wrapper<C>, int}]’
example.cpp:18:73: required from here
/usr/include/c++/4.8/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (C::*)(int)>(std::reference_wrapper<C>, int)>’
typedef typename result_of<_Callable(_Args...)>::type result_type;
^
/usr/include/c++/4.8/functional:1727:9: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (C::*)(int)>(std::reference_wrapper<C>, int)>’
_M_invoke(_Index_tuple<_Indices...>)
^

...全文
900 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
idzeta 2014-05-30
  • 打赏
  • 举报
回复
勘误:
引用 1 楼 idzeta 的回复:
目前,你可以用传指针的方式:
std::thread(&C::increase_member, &bar, 1000) // 去掉一个多余的“)”
或者用std::bind、lambda等解决。
idzeta 2014-05-30
  • 打赏
  • 举报
回复
问题出在:
std::thread(&C::increase_member, std::ref(bar), 1000) // std::ref()
C++11的std::thread构造函数不支持std::ref的这种用法。 你可以认为这是C++11标准的一个缺陷(LWG #2219)。不幸的是GCC 4.8很积极地迎合了标准。 目前,你可以用传指针的方式:
std::thread(&C::increase_member, &bar), 1000)
或者用std::bind、lambda等解决。
tj807126663 2014-05-30
  • 打赏
  • 举报
回复
感谢两位网友的解答,非常感谢。
mujiok2003 2014-05-30
  • 打赏
  • 举报
回复
lamda
#include <iostream>       // std::cout
#include <atomic>         // std::atomic
#include <thread>         // std::thread
#include <vector>         // std::vector
 
struct C : std::atomic<int> {
  C() : std::atomic<int>(0) {}
  void increase_member (int n) { for (int i=0; i<n; ++i) fetch_add(1); }
};
 
int main ()
{
  std::vector<std::thread> threads;
 
  std::cout << "increase counter (bar) with 10 threads using member...\n";
  C bar;
  for (int i=1; i<=10; ++i)
    threads.push_back(std::thread([&bar](){bar.increase_member(1000);}));
 
  std::cout << "synchronizing all threads...\n";
  for (auto& th : threads) th.join();
 
  std::cout << "bar: " << bar << '\n';
 
  return 0;
}

64,662

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧