C++ C++;,linux,如何将非静态方法从单例传递到pthread_create?

C++ C++;,linux,如何将非静态方法从单例传递到pthread_create?,c++,linux,multithreading,g++,pthreads,C++,Linux,Multithreading,G++,Pthreads,我在从singleton类线程化非静态方法时遇到问题,请查看代码: //g++ main.cc -lpthread #include<iostream> #include<unistd.h> #include<pthread.h> class SLAYER{ private: SLAYER(){}; ~SLAYER(){}; static SLAYER *singleton; pthrea

我在从singleton类线程化非静态方法时遇到问题,请查看代码:

//g++ main.cc -lpthread
#include<iostream>
#include<unistd.h>                 
#include<pthread.h>

class SLAYER{
  private:
    SLAYER(){};
    ~SLAYER(){};
    static SLAYER *singleton;
    pthread_t t1, t2;

  public: 
    static void *born(){
      singleton = new SLAYER;
    };
    static void *death(){
      delete singleton;
      singleton = NULL;
    };
    static void *start(){
      pthread_create(&singleton->t1,NULL,singleton->KerryRayKing, NULL);
      pthread_create(&singleton->t2,NULL,singleton->JeffHanneman, NULL);
    };
    void *JeffHanneman(void *arg){
      sleep(1);
      std::cout << "(1964-2013) R.I.P.\n";
      (void) arg;
      pthread_exit(NULL);
    };
    static void *KerryRayKing(void *arg){
      sleep(1);
      std::cout << "(1964-still with us) bald\n";
      (void) arg;
      pthread_exit(NULL);
    };
};

SLAYER *SLAYER::singleton=NULL;

int main(){
  SLAYER::born();
  SLAYER::start();
  std::cout << "thread started\n";
  sleep(5);
  SLAYER::death();
  return 0;
}
我试了几次,但都失败了。。。在这个cas中不可能使用非静态方法吗

编辑:
我忘了说,我不想允许从外部访问
JeffHanneman()

简短回答:你不能这么做

有几种解决方法,最简单的是使用
静态
包装函数,例如

static void *JHWrapper(void *self)
{
   SLAYER *that = static_cast<SLAYER*>(self);
   return that->JeffHanneman(); 
}

void *JeffHanneman(){   // Note "arg" removed.
  sleep(1);
  std::cout << "(1964-2013) R.I.P.\n";
  pthread_exit(NULL);
};
静态void*JHWrapper(void*self)
{
杀戮者*that=静态施法(自我);
返回->JeffHanneman();
}
void*JeffHanneman(){//注释“arg”已删除。
睡眠(1);
std::coutt1,NULL,SLAYER::JHWrapper,static_cast(单例));

[我没有使用“JHRapper”的双关语,因为我认为这相当有辱人格…][/p>简短回答:你不能这么做

有几种解决方法,最简单的是使用
静态
包装函数,例如

static void *JHWrapper(void *self)
{
   SLAYER *that = static_cast<SLAYER*>(self);
   return that->JeffHanneman(); 
}

void *JeffHanneman(){   // Note "arg" removed.
  sleep(1);
  std::cout << "(1964-2013) R.I.P.\n";
  pthread_exit(NULL);
};
静态void*JHWrapper(void*self)
{
杀戮者*that=静态施法(自我);
返回->JeffHanneman();
}
void*JeffHanneman(){//注释“arg”已删除。
睡眠(1);
std::coutt1,NULL,SLAYER::JHWrapper,static_cast(单例));


[我没有使用“JHRapper”的双关语,因为我认为这会有失身份…][/p>使用类静态或全局帮助器方法。顺便说一句,尝试将线程句柄的所有权封装在你的单例类中。你可能想使用
std::thread
(如果你启用了C++11)或“代码>升迁::线程< /COD>代替原始的PrPo.注意,pthRead创建有一个C函数指针。它与C++成员函数不兼容。我不知道Boost和STD::线程不是PthRead的包装器,使用类静态或全局帮助方法。BTW,尝试封装S中线程句柄的所有权。ingleton类。您可能希望使用
std::thread
(如果启用了C++11)或代码< > Boo::线程< /COD>代替原始的pHelp。注意,pthRead创建有C函数指针。它与C++成员函数不兼容。这里不知道Boost和STD::线程不是PthRead RealType的CAST的包装。StasyType将做和应该做的更好。甚至<代码>静态引用> <代码> >没有必要的,因为<代码> sLead */Cube是隐式转换为<代码> Value*/Cuth>。在两个方向上?C++中?我认为隐式转换只是C?好的,“马蒂彼得森:我觉得很愚蠢,因为在我的例子中,我只需要放<代码> jffHhanman()。作为私下的静态……不管怎样,你的代码让我感兴趣,我以前从未见过静态演员。我必须承认我不理解你的代码,尤其是在静态方法中,我需要做一些搜索……哦,我不知道谁是说唱歌手,我猜是说唱歌手…@CrazyEddie:你是“疯狂的Eddie'sGUI”的人吗?@bob'swoodenleg:JHRapper是个笑话-JeffHanneman当然不是说唱歌手,但函数名为JHWrapper(JeffHanneman Wrapper),发音与JHRapper相同-这是一种文字游戏形式。你会发现程序员经常这样做。JHWrapper函数中的
*this
只是一个变量名,它可以被称为
x
that
或其他名称。
static\u cast
用于从一种类型转换为另一种类型(在某些限制范围内,如果您试图“错误地”进行编译,编译器将反对)在这里,不需要重新解释,甚至是“代码”> StaseCase<代码>是不必要的,因为<代码> Stult*/Cuth>隐式转换为<代码> Value>代码>。在两个方向上?C++中,我认为隐式转换只是C?好的,“马蒂彼得森:我觉得很愚蠢,因为在我的例子中,我只是。需要把
JeffHanneman()
作为静态的私人…不管怎样,你的代码让我感兴趣,我以前从未见过静态演员。我必须承认我不理解你的代码,尤其是在静态方法中,我需要做一些搜索…哦,我不知道谁是说唱歌手,我猜是说唱歌手…@CrazyEddie:你是“疯狂的Eddie的GUI”?@bob'swoodenleg:JHRapper是一个笑话-JeffHanneman当然不是说唱歌手,但函数名为JHRapper(JeffHanneman Wrapper),发音与JHRapper相同-这是一种文字游戏形式。你会发现程序员经常这样做。JHWrapper函数中的
*this
只是一个变量名,它可以被称为
x
that
或其他名称。
static\u cast
用于从一种类型转换为另一种类型(在某些限制范围内,如果您试图“错误地”进行编译,编译器将反对)。
 pthread_create(&singleton->t1,NULL, SLAYER::JHWrapper, static_cast<void *>(singleton));