Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用pthread调用对象的成员函数_C++_Pthreads - Fatal编程技术网

C++ 使用pthread调用对象的成员函数

C++ 使用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

如何使用pthread将thread_ready_函数调用到已注释的线程中?我需要用类对象调用它,在现实世界中,函数使用以前设置的属性

MWE


如果不想使用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; 
}