如何通过boost线程调用函数指针
Posted
技术标签:
【中文标题】如何通过boost线程调用函数指针【英文标题】:how to call a function pointer via boost thread 【发布时间】:2015-02-05 05:47:07 【问题描述】:class A
public:
int xx(int size)
public:
int xx(int size)
int yy(int size)
;
int main()
typedef int (A::*functions)(int);
std::vector<functions> methods =
&A::xx,
&A::yy
;
A aa;
boost::thread_group thgrp;
for (typename std::vector<functions>::iterator itr=methods.begin();itr!=methods.end();++itr)
functions z=*itr;
boost::thread *t=new boost::thread(z,aa,10);
thgrp.add_thread(t);
thgrp.join_all();
return 0;
我收到一个错误“没有匹配的函数调用'get_pointer'”。 我想使用 boost 线程从我的预定义类型的向量中调用方法。
请帮我解决这个问题!
【问题讨论】:
也许发送&aa
?
...这不是有效的 C++ 代码。 A
类有两个成员,它们具有相同的名称 (xx
) 和签名 (int
)。 xx
和 yy
都没有返回任何内容,但被声明为这样做。
【参考方案1】:
对不起!!!
我发现我做错了!此代码运行正常! 我用对象“方法”而不是类“aa”的对象调用 boost::thread。
【讨论】:
以上是关于如何通过boost线程调用函数指针的主要内容,如果未能解决你的问题,请参考以下文章
在 boost 线程上调用抽象函数,不会在 interrupt_point 中断
从多个线程调用 boost::asio::io_service 运行函数