我是C++
的新手,正在尝试使用pthread
创建多个线程。
typedef struct thread_args{
int &sockfd;
struct sockaddr_in &serv_addr;
int size_serv_addr;
socklen_t &clilen;
int &newsockfd;
};
void create_server(int &sockfd, struct sockaddr_in &serv_addr, int size_serv_addr, socklen_t &clilen, int &newsockfd){
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd;
socklen_t clilen;
pthread_t t1;
struct sockaddr_in serv_addr, cli_addr;
struct thread_args *args;
args->clilen = clilen;
args->newsockfd = newsockfd;
args->serv_addr = serv_addr;
args->size_serv_addr = sizeof(serv_addr);
args->sockfd = sockfd;
pthread_create(&t1, NULL, create_server, &args);
printf("hello abc");
return 0;
}
当我运行此代码时,它会显示一条消息:
error:/bin/sh -c 'make -j 4 -e -f error: invalid conversion from 'void* (*)(int&, sockaddr_in&, int, socklen_t&, int&) {aka void* (*)(int&, sockaddr_in&, int, unsigned int&, int&)}' to 'void* (*)(void*)' [-fpermissive]
pthread_create(&t1, NULL, create_server, &args);
我怎样才能解决这个问题?
最佳答案
您的线程函数的签名应为:
void *(*start_routine) (void *)
但您提供:
void create_server(int &sockfd, struct sockaddr_in &serv_addr, int size_serv_addr, socklen_t &clilen, int &newsockfd)
您应该创建一个类似以下的函数:
void* myThread(void *arg);
然后
args
调用中的pthread_create
参数将作为arg
参数传递给myThread
,您可以使用其字段来调用create_server
关于c++ - pthread c++从void *(*)到void *(*)(void *)的无效对话,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39771441/