fsidiosidi
fsidiosidi

Reputation: 51

How can I fix the error in this Pthread example?

I did all the necessary configurations, like including Pthread library and header files...

The error is:

error C3867: 'FunctionClass::calledfunction': function call missing argument list; use '&FunctionClass::calledfunction' to create a pointer to member

Here's the example that causes the error:

class FunctionClass
{

 public:

    void * calledfunction( void *);
    /*
    this is the definition of the function in the FunctionClass.cpp
    void * FunctionClass::calledfunction( void *)
    {
        //do sth;
    }

    */


};

int main(void)
{

pthread_t process_m;
FunctionClass *obj = new FunctionClass ();

int nbr= 5;

if(pthread_create(&process_m,NULL,obj->calledfunction,(void *)nbr)< 0)
    {
        std::cout << "thread1";

}
 }

What could cause the error? I respected the syntax of the function pthread_create... But I can't find the reason for this error!

Upvotes: 0

Views: 1164

Answers (3)

UmmaGumma
UmmaGumma

Reputation: 5693

calledfunction must be static or non class function.

Edit: If you need to call nonstatic function do following.

struct forFunctionCalling
{
    FunctionClass * ptr;
    void * otherarguments;
};

void * somefunction (void * v)
{
    forFunctionCalling * f_ptr = reinterpret_cast<forFunctionCalling>(v);
    return f_ptr->ptr->calledfunction(f_ptr->otherarguments);
}

And call somefunction instead calledfunction. P.S. Of course this code will look much better if you change type otherarguments from void * to it's real type

Upvotes: 2

peoro
peoro

Reputation: 26060

You cannot use non-static member functions as callback.

Use a free function or a static member function as third parameter of pthread_create.


Edit to reflect OP's comment:

If you need to call a function member of FunctionClass for a specific FunctionClass object (obj in your example) the common way is that of calling a static member function (or a free one) passing your object to it, and then call object's member function from there.

Here an example (didn't test it, but it should let you understand what to do):

struct obj_arg_pair { FunctionClass *obj; int nbr; };
static void * calledfunctionStatic( void *args_ )
{
    obj_arg_pair *args = reinterpret_cast< obj_arg_pair * >( args_ );
    return args->obj->calledFunction( args->nbr );
}

and then start your thread with a code similar to this one:

obj_arg_pair args;
args.nbr = 5;
args.obj = obj;
pthread_create(&process_m,NULL,FunctionClass::calledfunction,(void *)&args);

Upvotes: 5

CashCow
CashCow

Reputation: 31425

You need to do as the others here have suggested: create an "object" to hold all the data your function needs then pack that into the "void *" parameter. The function should be a free-function or a static class member function - the static member function if it needs access to private members of the class.

What I will add is how you must handle the lifetime of the objects you pack into your "void *" holder. You can:

  • use "new" to create it. You will know then it exists but must delete it at some point. You cannot pass shared_ptr to your thread-function but you can put shared_ptr in your thread-function itself if you know it was created for you with "new". You could also make the deleter one of the members of the packed struct, and use that in boost::shared_ptr so at call time you indicate how to clean up the data.

  • use wait mechanisms so the caller waits for the thread to "accept" the data before exiting where the objects will lose scope. The thread "accepts" after making copies of this data for its use. That way you can create a local struct of the data and pass that in.

You might consider using boost::thread although where you can pass in shared_ptr objects as parameters to boost::bind and write your function in a way to receive these.

Upvotes: 1

Related Questions