-1

I was previously using a void* function as the third parameter of pthread_create, and here is what it looked like:

void* nameChange(void*){ ... }
...
pthread_t id;
pthread_create(&id, NULL, nameChange, NULL);

This worked. But I have made some changes to my code and need the function nameChange to be a member of my class MainWindow. Here's the only difference now:

void* MainWindow::nameChange(void*)

Now, I get an error when putting nameChange as the parameter. Here's what it says:

error: cannot convert 'MainWindow::nameChange' from type 'void* (MainWindow::)(void*)' to type 'void* (*)(void*)'

What is it that I am doing wrong here? I'm pretty new to threads, so any help is appreciated!

2

1 Answer 1

2

The difference between a C function and a C++ member function is that C function uses cdecl calling convention, while member functions uses thiscall calling convention. You can't call the member function directly. Member function pointers are not the same type as function pointers.

Maybe here is one workaround

void* callback(void*)
{
    MainWindow instance;
    instance.nameChange();
}

pthread_create(&id, NULL, callback, NULL);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.