C++11 使用代理将成员函数传递给线程
C++11 pass a member function to a thread using a proxy
我尝试生成一种通用方法来创建具有 argunment a class 方法的线程。但是我无法成功编译代码我使用以下代码
#include <iostream>
#include <thread>
#include <functional>
using namespace std;
class hello{
public:
void f(){
cout<<"f"<<endl;
}
virtual void ff(){
cout<<"ff"<<endl;
}
};
template <typename T, T> struct proxy;
template <typename T, typename R, typename ...Args, R (T::*mf)(Args...)>
struct proxy<R (T::*)(Args...), mf>
{
static R call(T & obj, Args &&... args)
{
// function func = T::*mf;
thread t(&T::*mf, &obj);
return (obj.*mf)(std::forward<Args>(args)...);
}
};
int main(){
hello obj;
typedef proxy<void(hello::*)(), &hello::f> hello_proxy;
hello_proxy::call(obj);
}
编译时生成以下错误
In static member function 'static R proxy<R (T::*)(Args ...), mf>::call(T&, Args&& ...)': 24:22: error: expected unqualified-id before '*' token
thread t(&T::*mf, &obj);
&T::*mf
是错误的语法。 只需使用mf
.
thread t(mf, &obj);
相关文章:
- C++使用params创建线程函数会导致转换错误
- 如何在大函数中编写多线程函数?
- C++11:将矢量元素作为线程传递到线程函数中
- 线程函数中的c++参数更改
- 我需要线程函数在不停止实际程序的情况下,每2秒打印一次随机数
- 在多线程函数中返回共享的常量指针会导致计时问题吗?
- 将项目插入 CTreeCtrl 时线程函数的奇怪行为
- C++ 线程函数通过引用传递
- 线程函数参数的前向声明不起作用
- 线程函数无法从堆上分配的缓冲区中读取字符
- 线程函数不打印
- C :如何在复杂模板类中指定线程函数
- 为什么我在C++线程函数调用中得到重复值?
- C++ 线程函数指针实现返回错误无效使用非静态成员函数
- 线程函数中的通用引用
- 如何同时启动两个具有不同线程函数的线程团队
- std::c++ 中的线程在设置线程函数时不会出错
- 对抽象类的引用不能传递给线程函数?
- 如何将ifstream作为参数传递给std ::线程函数
- 如何线程函数