Passing a C++ function object to pthread_create function as the thread routine

后端 未结 1 366
眼角桃花
眼角桃花 2021-01-14 14:29

I know the thread routine that is passed to pthread_create API has the prototype of

void *threadproc(void *).

I was just wondering if it i

相关标签:
1条回答
  • 2021-01-14 15:00

    A pthread function must have C linkage, so it cannot be a member function. Strictly speaking, it cannot even be a static member function, even though that will work almost all the time.

    So what should be done is to create a non-member function that takes a void* argument that will be a pointer to the C++ object to be the thread function. That function can cast the void* argument to a class pointer, which calls the member function.

    See In C++, is it safe/portable to use static member function pointer for C API callbacks?

    0 讨论(0)
提交回复
热议问题