Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.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
Pthreads 方法中的C++ pthRead创建_Pthreads - Fatal编程技术网

Pthreads 方法中的C++ pthRead创建

Pthreads 方法中的C++ pthRead创建,pthreads,Pthreads,有没有一种简单的方法可以在每次调用方法时创建一个新的pthread 我有一个在特定情况下激活的方法,这是与其他程序通信的唯一方法。我需要进行睡眠,并在调用所述方法后执行另一个方法,在等待期间可以选择另一个调用-这就是我希望使用线程的原因 我想使用标准: pthread_create(&new_thread, NULL, threadbody() ); 这样说 std::vector<phtread_t> thread(20) ... pthread_t new_threa

有没有一种简单的方法可以在每次调用方法时创建一个新的pthread

我有一个在特定情况下激活的方法,这是与其他程序通信的唯一方法。我需要进行睡眠,并在调用所述方法后执行另一个方法,在等待期间可以选择另一个调用-这就是我希望使用线程的原因

我想使用标准:

 pthread_create(&new_thread, NULL, threadbody() );
这样说

std::vector<phtread_t> thread(20)
...
pthread_t new_thread;
int rc;
rc = pthread_create(&new_thread, NULL, threadbody() );
threads.push_back(new_thread);

我做错了什么?

您的函数是类的非静态成员。这是一个禁忌,因为pthread_create只适用于C领域

要抵消这一点,您可以

class X
{
public:
  static void* callback(void* p)
  {
    X* x = reinterpret_cast<X*>(p);
    x->function();
    return 0;
  }

  void function(void)
  {
     // do work here
  }
};

X obj;
pthread_create(&new_thread, &obj, &X::callback);

我还遇到了发送到pthread_create的属性的问题,我应该如何正确地调用它?假设我想发送INT attribute,您可以让您的属性成为类X的公共成员。然后,在回调中,您可以根据需要使用它。进入函数后,可以访问任何成员对象和函数。
class X
{
public:
  static void* callback(void* p)
  {
    X* x = reinterpret_cast<X*>(p);
    x->function();
    return 0;
  }

  void function(void)
  {
     // do work here
  }
};

X obj;
pthread_create(&new_thread, &obj, &X::callback);