C++ 使用pthread调用对象的成员函数
如何使用pthread将thread_ready_函数调用到已注释的线程中?我需要用类对象调用它,在现实世界中,函数使用以前设置的属性 MWEC++ 使用pthread调用对象的成员函数,c++,pthreads,C++,Pthreads,如何使用pthread将thread_ready_函数调用到已注释的线程中?我需要用类对象调用它,在现实世界中,函数使用以前设置的属性 MWE 如果不想使用C++11、stl或boost,则必须为成员函数使用静态关键字,以便pthread可以调用成员函数! 示例代码: #include <iostream> #include <pthread.h> using namespace std; class A{ public: static void* thr
如果不想使用C++11、stl或boost,则必须为成员函数使用静态关键字,以便pthread可以调用成员函数! 示例代码:
#include <iostream>
#include <pthread.h>
using namespace std;
class A{
public:
static void* thread(void* args);
int parella_thread(int thread_num);
};
void* A::thread(void* args)
{
cout<<"hello world"<<endl;
}
int A::parella_thread(int thread_num)
{
pthread_t* thread_ids = new pthread_t[thread_num];
for(int i=0;i<thread_num;i++)
{
pthread_create(&thread_ids[i],NULL,thread,(void*)NULL);
}
delete[] thread_ids;
}
int main(int argc,char*argv[])
{
A test;
test.parella_thread(4);
return 0;
}
你能用C++ 11线程吗?我看C++ 11。但因为它的支持很棘手,我想知道是否有一种方法可以使用pthread实现这一点
#include <iostream>
#include <pthread.h>
using namespace std;
class A{
public:
static void* thread(void* args);
int parella_thread(int thread_num);
};
void* A::thread(void* args)
{
cout<<"hello world"<<endl;
}
int A::parella_thread(int thread_num)
{
pthread_t* thread_ids = new pthread_t[thread_num];
for(int i=0;i<thread_num;i++)
{
pthread_create(&thread_ids[i],NULL,thread,(void*)NULL);
}
delete[] thread_ids;
}
int main(int argc,char*argv[])
{
A test;
test.parella_thread(4);
return 0;
}