Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ pthread_创建问题_C++_Gcc_Pthreads - Fatal编程技术网

C++ pthread_创建问题

C++ pthread_创建问题,c++,gcc,pthreads,C++,Gcc,Pthreads,我有以下代码: void* ConfigurationHandler::sendThreadFunction(void* callbackData) { const EventData* eventData = (const EventData*)(callbackData); //Do Something return NULL; } void ConfigurationHandler::sendCancel() { EventData* eventData =

我有以下代码:

void* ConfigurationHandler::sendThreadFunction(void* callbackData)
{
   const EventData* eventData = (const EventData*)(callbackData);

   //Do Something

   return NULL;
}

void ConfigurationHandler::sendCancel()
{
    EventData* eventData = new EventData();
    eventData ->Name = "BLABLA"

    pthread_t threadId = 0;
    int ret = pthread_create(&threadId,
                             NULL,                                                              
                             ConfigurationHandler::sendThreadFunction,
                             (void*) eventData );                                   // args passed to thread function
    if (ret)
    {
        log("Failed to launch thread!\n");
    }
    else
    {
        ret = pthread_detach(threadId);
    }   
}
我收到一个编译器错误:

error: argument of type 'void* (ConfigurationHandler::)(void*)' does not match 'void* (*)(void*)'

您不能安全地将C++方法——甚至静态方法——作为一个例程传递给<代码> pthRead创建< /C> > < /P> 假设不传递对象,即,
ConfigurationHandler::sendThreadFunction
声明为静态方法:

// the following fn has 'C' linkage:

extern "C" {

void *ConfigurationHandler__fn (void *arg)
{
    return ConfigurationHandler::sendThreadFunction(arg); // invoke C++ method.
}

}

将作为参数传递给PthRead创建/<代码> .p/>

您的问题的典型方法是通过VC++对象(通过它的接口中的数据指针)将p++对象传递给pthRead创建()。传递的线程函数将是全局的(可能的静态函数),它知道空穴指针实际上是C++对象。 如本例所示:

void ConfigurationHandler::sendThreadFunction(EventData& eventData)
{
   //Do Something
}

// added code to communicate with C interface
struct EvendDataAndObject {
   EventData eventData;
   ConfigurationHandler* handler;
};
void* sendThreadFunctionWrapper(void* callbackData)
{
   EvendDataAndObject* realData = (EvendDataAndObject*)(callbackData);

   //Do Something
   realData->handler->sendThreadFunction(realData->eventData);
   delete realData;
   return NULL;
}

void ConfigurationHandler::sendCancel()
{
    EvendDataAndObject* data = new EvendDataAndObject();
    data->eventData.Name = "BLABLA";
    data->handler = this; // !!!

    pthread_t threadId = 0;
    int ret = pthread_create(&threadId,
                             NULL,                                                              
                             sendThreadFunctionWrapper,
                             data ); 
    if (ret)
    {
        log("Failed to launch thread!\n");
    }
    else
    {
        ret = pthread_detach(threadId);
    }   
}

你不能安全地将C++方法——甚至静态方法——作为一个例程传递到<代码> pthRead创建/<代码>。有什么更好的方法?我怎么才能达到上述的功能性?为什么你不能安全地传递C++静态方法或函数?@ SLVIK262:因为C和C++调用约定可以不同。从C中声明未被声明为“C”的C++函数也不安全。