如何通过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 线程从我的预定义类型的向量中调用方法。

请帮我解决这个问题!

【问题讨论】:

也许发送&amp;aa ...这不是有效的 C++ 代码。 A 类有两个成员,它们具有相同的名称 (xx) 和签名 (int)。 xxyy 都没有返回任何内容,但被声明为这样做。 【参考方案1】:

对不起!!!

我发现我做错了!此代码运行正常! 我用对象“方法”而不是类“aa”的对象调用 boost::thread。

【讨论】:

以上是关于如何通过boost线程调用函数指针的主要内容,如果未能解决你的问题,请参考以下文章

c++线程函数中如何调用该方法下的外部函数?

如何在线程中调用其他类的成员函数和变量

c++中如何将子线程的参数传回主线程

类内定义线程的回调函数问题

如何通过成员函数指针调用成员函数? [复制]

调用newthread后如何获取线程索引?