-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!

Archie Gertsman
  • 1,601
  • 2
  • 17
  • 45

1 Answers1

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);
zangw
  • 43,869
  • 19
  • 177
  • 214