C++ 使用ptrhead奇怪地重现模板模式
因此,我试图弄清楚,您是否可以使用奇怪的循环模板模式来绕过pthread在类方法中使用的限制,甚至通过执行以下操作来创建类C++ 使用ptrhead奇怪地重现模板模式,c++,templates,crtp,C++,Templates,Crtp,因此,我试图弄清楚,您是否可以使用奇怪的循环模板模式来绕过pthread在类方法中使用的限制,甚至通过执行以下操作来创建类 template <class T> class thread_helper { static void* create(void *input) { T * output=new T; return static_cast<void*>(output); } static void* using(void *i
template <class T>
class thread_helper
{
static void* create(void *input)
{
T * output=new T;
return static_cast<void*>(output);
}
static void* using(void *input)
{
T::use(input);
}
};
class user : public thread_helper<user>
{
void method(int x)
{
//does something
}
static void use(void* input)
{
this->method(static_cast<int>(input));
}
};
注意:上面的代码中有很多错误。请不要为他们把我撕碎。我真的只是想画一幅我想做的事情的图画。我还试图找出是否有更好的方法来做这个手术
另外,第二个
pthread\u create
方法真的有必要吗?难道我不能也使用构造函数吗?我将发布一个工作测试代码。它涉及一吨锅炉板代码。它看起来很难看,但很管用。注意:我可能会在以后更改thread\u helper的create方法,以便它不能用于线程
#include <iostream>
#include "pthread.h"
using namespace std;
template <class I>
struct helper
{
void *(I::*method)(void *); //enables you to call and get information from any non static class method
I *ptr; //this is the class pointer used to call the member pointer
void *info; //this is what you pass into the member pointer
};
template <class T>
class thread_helper
{
public:
static void *create(void *in)
{
T * output=new T;
return static_cast<void*>(output);
}
static void *method(void *in)
{
helper<T> *utilize = static_cast<helper<T> *>(in);
return (utilize->ptr->*utilize->method)(utilize->info);
}
};
class test: public thread_helper<test>
{
public:
void *test_method(void *in)
{
int *val = (int *)in;//note: not static_casting since this is not a class
*val *= 4;
return val; //auto casts to void *
}
};
int main()
{
//initialize test
test * second = static_cast<test*>(test::create(NULL));
//initialize helper
helper<test> first;
first.ptr = second;
first.method = &test::test_method;
int val = 4;
first.info = &val;//
//setup threads
pthread_t thread;
void *ans;
//call test_method and retrieve answer
pthread_create(&thread, NULL, test::method, static_cast<void *>(&first));
pthread_join(thread, &ans);
cout << "the answer is "<< *(int *)ans <<endl;
return 0;
}
#包括
#包括“pthread.h”
使用名称空间std;
模板
结构辅助程序
{
void*(I::*方法)(void*);//使您能够调用任何非静态类方法并从中获取信息
I*ptr;//这是用于调用成员指针的类指针
void*info;//这是传递给成员指针的内容
};
模板
类线程辅助程序
{
公众:
静态void*创建(void*in)
{
T*输出=新的T;
返回静态_cast(输出);
}
静态void*方法(void*in)
{
helper*use=static_cast(in);
返回(利用->ptr->*利用->方法)(利用->信息);
}
};
类测试:公共线程\u助手
{
公众:
void*test_方法(void*in)
{
int*val=(int*)in;//注意:由于这不是一个类,所以不是静态的\u转换
*val*=4;
return val;//自动强制转换为void*
}
};
int main()
{
//初始化测试
test*second=static_cast(test::create(NULL));
//初始化帮助程序
助手优先;
first.ptr=第二;
first.method=&test::test\u方法;
int-val=4;
first.info=&val//
//设置线程
pthread\u t线程;
无效*ans;
//调用test_方法并检索答案
pthread_create(&thread,NULL,test::method,static_cast(&first));
pthread_连接(线程和ans);
总之,典型的范例是将类实例地址转换为Value*,并将其作为线程参数传递给一个线程进程,将其作为相同的指针返回。如果您有C++ 11,考虑使用STD::Type。“如果有更好的方法来执行这个操作”-在一个单独的线程中运行构造函数通常没有什么用处,除非你有一些同步的方法来访问对象-我建议你阅读一下等人的资料。@TonyD我签出了std::future。这让我很恼火,因为我不能在这个项目中使用新的标准。我也同意你的观点,不确定构造函数部分实际上需要是读过了。不管怎样,我最终得到了一个代码,它工作了,但它是一个大量的锅炉板代码。你可以使用重新解释\u cast
而不是C-cast。@Jarod42重新解释\u cast不危险吗?C-cast更危险,因为它不明确。重新解释\u cast
是明确的(但仍然危险)。
pthread_create(thread_variable, NULL, user::using(), void);
#include <iostream>
#include "pthread.h"
using namespace std;
template <class I>
struct helper
{
void *(I::*method)(void *); //enables you to call and get information from any non static class method
I *ptr; //this is the class pointer used to call the member pointer
void *info; //this is what you pass into the member pointer
};
template <class T>
class thread_helper
{
public:
static void *create(void *in)
{
T * output=new T;
return static_cast<void*>(output);
}
static void *method(void *in)
{
helper<T> *utilize = static_cast<helper<T> *>(in);
return (utilize->ptr->*utilize->method)(utilize->info);
}
};
class test: public thread_helper<test>
{
public:
void *test_method(void *in)
{
int *val = (int *)in;//note: not static_casting since this is not a class
*val *= 4;
return val; //auto casts to void *
}
};
int main()
{
//initialize test
test * second = static_cast<test*>(test::create(NULL));
//initialize helper
helper<test> first;
first.ptr = second;
first.method = &test::test_method;
int val = 4;
first.info = &val;//
//setup threads
pthread_t thread;
void *ans;
//call test_method and retrieve answer
pthread_create(&thread, NULL, test::method, static_cast<void *>(&first));
pthread_join(thread, &ans);
cout << "the answer is "<< *(int *)ans <<endl;
return 0;
}