- 警告和pthread_create

时间:2012-07-28 14:16:30

标签: c++ pthreads ansi iso

我是ansi和iso世界的新手,我编写了我的程序:

-asni -pedantic -std=c++11 -std=c++98 

现在,我收到以下警告:

 warning: converting from 'void (NetworkSocket::*)()' to 'void* (*)(void*)' [-pedantic]

以下一行:

if (pthread_create(this->threadArray, NULL, (void*(*)(void*)) &NetworkSocket::threadProcedure  , (void *)this) != 0)
        { /* error */      }

我如何通过迂腐警告?

1 个答案:

答案 0 :(得分:2)

pthread_create是一个C函数,期望C函数获取一个void指针,并返回一个void指针。因此,如果使用this指针作为线程参数,则可以使用这样的C函数将调用分派给您的成员函数:

extern "C" void* startThread( void* p )
{
    static_cast< NetworkSocket* >( p )->threadProcedure();

    return 0;
} 

if ( pthread_create( this->threadArray, 0, startThread, this ) )
   ...