2

I am a little confused on how pthread works - specifically, I am pretty sure that pthread takes in a pointer to a function that takes a void pointer as an argument (correct me if I am wrong), and I have declared my function in that way, but I am still getting an error. Here is the code I am struggling with:

void eva::OSDAccessibility::_resumeWrapper(void* x)
{
    logdbg("Starting Connection.");
    _listener->resume();
    logdbg("Connected.");
    pthread_exit(NULL);
}

void eva::OSDAccessibility::resumeConnection()
{    
    long t;
    _listener->setDelegate(_TD);
    pthread_t threads[1];
    pthread_create(&threads[0], NULL, &eva::OSDAccessibility::_resumeWrapper, (void *)t);

}

The error I'm getting is:

No matching function for call to pthread_create.

You don't necessarily have to tell me how to fix the code (although that would be appreciated of course), I'm more interested in why this error is coming up and if my understanding of pthread is correct. Thanks! :)

2
  • 2
    Are you using C or C++? If you are using C++ I suggest you use the standard thread library Commented Jun 27, 2016 at 15:24
  • Thanks for the suggestion! And I think I may understand what's going on. We can't actually use pthread on a member function within a class. A better way of doing this would be to create an instance of my class in main() and call pthread_create from there, I would imagine ... Commented Jun 27, 2016 at 15:36

1 Answer 1

2
  1. Your function signature must be void * function (void*)

  2. If called from c++ code, the method must be static:

    class myClass
    {
        public:
        static void * function(void *);
    }
    

A solution to use methods that are not static is the following:

class myClass
{ 
    // the interesting function that is not an acceptable parameter of pthread_create
    void * function();

    public:
    // the thread entry point
    static void * functionEntryPoint(void *p)
    {
        ((myClass*)p)->function();
    }
}

And to launch the thread:

myClass *p = ...;
pthread_create(&tid, NULL, myClass::functionEntryPoint, p);
Sign up to request clarification or add additional context in comments.

2 Comments

How about extern C? Does have one have to specify it?
Okay thanks! I think I get my problem now, I was trying to call a non-static member function, which isn't possible in pthread. This helps, thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.