Error regarding 3rd parameter of pthread_create()

Could someone please explain to me the return type of pthread_create()'s 3rd parameter? I know it should be a function but I keep getting the error: argument of type 'void' (Test::)(void*)' does not match 'void * (*) (void*)'.

Here is the function that I pass to pthread_create():
1
2
3
void * Test::run(void * arg)
{   cout << "Running...";
}



And this is how I call pthread_create():
 
pthread_create(&aThread,NULL,run,NULL);
You can't pass a member function to pthread_create. You need to pass a helper function that takes a pointer to the object that has the run() function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Test::run() {
    cout << "Running";
}

// ...

void* start_thread(void* arg) {
    static_cast<Test*>(arg)->run();
}

// ...

Test aTest;
pthread_create(&aThread, NULL, start_thread, &aTest);

Topic archived. No new replies allowed.