8
class Scoreget{
    private:
        //some variables
    public:
        Scoreget(){
            //something here
        }

        void* basicgetscore(){
            //somthing here
        }

        void getscore(Scoreget s){
            pthread_t t;
            if(pthread_create(&t,NULL,s.basicgetscore,NULL)==-1){
               printf("Error 3\n");
               exit(3);
            }
            void *a;
            if(pthread_join(t,&a)==-1){
               printf("Error \n);
               exit(4);
            }
        }
};

I am trying to run a separate thread for calling a function,because it uses a call to execl(), and thus will stop my program (I am on windows and cannot use fork()). Combining threading with classes is giving me hard times.

From some googling I understood I need to make that last function friend or static and use some sort of this pointer. I have tried on my part but pieces are not fitting together. I am even unable to change the error type. Its frustrating me now. Getting same error:

cannot convert Scoreget::basicgetscore from type void*(Scoreget::)() to type void* (*) (void *)

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
vish213
  • 748
  • 3
  • 12
  • 27
  • duplicate of http://stackoverflow.com/questions/1151582/pthread-function-from-a-class – TJD Aug 20 '12 at 18:40
  • 2
    @^ : I honestly searched everywhere. Now if people don't properly include error which we search for on encountering, how would I ever know that post exist? That error could have came in title so search could have shown something to me. :-/ – vish213 Aug 20 '12 at 18:43
  • There are loads and loads of duplicates of this question. – Jonathan Wakely Aug 20 '12 at 19:47

3 Answers3

9

Declare it static, and add another helper method:

static void* basicgetscore(void * object){
    return ((Scoreget *) object)->dobasicgetscore();
}

void * dobasicgetscore() // new helper method
{
    // do stuff here
    return NULL;
}

create pthread with:

if (pthread_create(&t,NULL,basicgetscore,&s) == -1) { ...

Also, be very careful. You're giving this thread the address of a temporary variable. It's safe in this specific case because the pthread_join is in the same function, but if you were to return from the function before the thread exits, you'll deallocate the object you're running the thread inside, which could result in all kinds of nasty behavior. Consider updating your function to either 1) take a reference or pointer or 2) operate on this.

Wug
  • 12,956
  • 4
  • 34
  • 54
7

You can't pass non static method to pthread_create

The most simple way, create static method which would run basicgetscore

Something like this

static void *Scoreget::basicgetscore_starter(void *p) {
    Scoreget *t = (Scoreget *)p;
    t->basicgetscore();
}

pthread_create(&t,NULL,&Scoreget::basicgetscore_starter,(void *)&s);
CyberDem0n
  • 14,545
  • 1
  • 34
  • 24
  • Means create a wrapper function which simply contains a call to basicgetscore(), lets call it static_basicgetscore(), now simply put name of this newly created function in pthread_create() ? – vish213 Aug 20 '12 at 18:40
  • Yes, and don't forget to pass &s as last argument of pthread_create – CyberDem0n Aug 20 '12 at 18:41
  • This is fine, but I think that you can also accomplish the same with std::mem_fn or boost::mem_fn magic - although getting the syntax right is not trivial. – Lubo Antonov Aug 20 '12 at 18:44
0

You need to define a non member (static) function to pass as thread function for pthread_create(). Referencing from your object doesn't help, since this is a function pointer that is expected from this function. You can pass a pointer to your object instance using the user args void* pointer though.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190