我在sending类中具有startSending过程和一个朋友函数(sender)。我想从新线程中调用friend函数,因此我在startSending过程中创建了一个新线程。

class sender{
    public:
    void startSending();

    friend void* sending (void * callerobj);
}

void sender::startSending(){
    pthread_t tSending;
    pthread_create(&tSending, NULL, sending(*this), NULL);
    pthread_join(tSending, NULL);
}

void* sending (void * callerobj){
}


但是我得到了这个错误

cannot convert ‘sender’ to ‘void*’ for argument ‘1’ to ‘void* sending(void*)’


从pthread_create调用发送的正确方法是什么?

最佳答案

pthread_create签名如下所示:

int pthread_create(pthread_t *thread, //irrelevant
                   const pthread_attr_t *attr, //irrelevant
                   void *(*start_routine) (void *), //the pointer to the function that is going to be called
                   void *arg); //the sole argument that will be passed to that function


因此,在您的情况下,必须将指向sending的指针作为第三个参数传递,而将this(将传递给sending的参数)作为最后一个参数传递:

pthread_create(&tSending, NULL, &sending, this);

关于c++ - C++无法将参数“1”的“发送者”转换为“无效*”,而将“1”转换为“无效*发送(void *)”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33119831/

10-11 15:36