Argument of type Void is incompatible pthread c++

I want the Run function to run its own thread, but it shows an error, how to make it compatible with each other

Error : argument of type "void (cMain::)(void *arg)" is incompatible with parameter of type "void *(__cdecl *)(void *)"


cMain.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#define HAVE_STRUCT_TIMESPEC
#include <pthread.h>

using std::string;
class cMain
{ 
public:
    cMain();
    ~cMain();

public:
    wxListView *Listview1 = nullptr;
    
    void *Run(void *arg);
    int main();
};


cMain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void * cMain::Run(void *arg)
{
    int i = 0;
    while (true)
    {
        i += 1;
        Listview1->SetItem(0, 0, std::to_string(i));

        Sleep(200);
    }

    pthread_exit(NULL);

    return 0;
}

int cMain::main()
{
    pthread_t my_thread;

    int ret;

    ret = pthread_create(&my_thread, NULL, &Run, NULL);
    if (ret != 0) {
        MessageBox(NULL, L"Error: pthread_create() failed", L"AA", MB_OK);
        exit(EXIT_FAILURE);
    }
}


error code : ret = pthread_create(&my_thread, NULL, &Run, NULL);
Last edited on
Using the C++ thread header file gives you a lot more options.
https://www.cplusplus.com/reference/thread/thread/

> Error : argument of type "void (cMain::)(void *arg)" is incompatible with parameter of type "void *(__cdecl *)(void *)"
The problem you have here is that pthread doesn't know anything about C++ classes, member variables or the 'this' pointer.

But you can do this with a static member function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class cMain
{ 
public:
    cMain();
    ~cMain();

public:
    wxListView *Listview1 = nullptr;
    
    void Run(void);
    static void Runner(void *arg);
    int main();
};


void cMain::Run(void)
{
    int i = 0;
    while (true)
    {
        i += 1;
        Listview1->SetItem(0, 0, std::to_string(i));

        Sleep(200);
    }

    pthread_exit(NULL);

    return 0;
}

static void cMain::Runner(void *arg) {
  cMain::self *p = reinterpret_cast<cMain*>(arg);
  p->Run();
}

int cMain::main()
{
    pthread_t my_thread;

    int ret;

    ret = pthread_create(&my_thread, NULL, &Runner, this);
    if (ret != 0) {
        MessageBox(NULL, L"Error: pthread_create() failed", L"AA", MB_OK);
        exit(EXIT_FAILURE);
    }
}


The purpose of the Runner is to cast the 'this' pointer back from void* into the correct type, then invoke the real member function to do the work.
hi Salem c !

cMain::self *p = reinterpret_cast<cMain*>(arg);
error : class cMain has no member self

I corrected it
cMain *p = reinterpret_cast<cMain*>(arg);

it shows an error
'int pthread_create(pthread_t *,const pthread_attr_t *,void *(__cdecl *)(void *),void *)': cannot convert argument 3 from 'void (__cdecl *)(void *)' to 'void *(__cdecl *)(void *)'
Last edited on
Oops - Runner still needs to return void*
Last edited on
Topic archived. No new replies allowed.