[英]can't create thread properly
can someone tell me what am I doing wrong? 谁能告诉我我做错了什么? (I omitted the rest of the program because its very long...) (我省略了程序的其余部分,因为它很长......)
#include <pthread.h>
void *RTPfun(char *client_addr);
int main(int argc, char *argv[])
{
char* client_addr;
pthread_t RTPthread;
// ...
pthread_create(&RTPthread, NULL, &RTPfun, client_addr)
}
void *RTPfun(char * client_addr)
{
// ...
return;
}
the error: 错误:
TCPserver.c: In function ‘main’: TCPserver.c:74:5: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [enabled by default] /usr/include/pthread.h:225:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘void * (*)(char *)’
Pthread works with functions that receive void* and return void*. Pthread使用接收void *并返回void *的函数。
You need to change the parameter of your function from char* to void*. 您需要将函数的参数从char *更改为void *。 Here's an alternative: 这是另一种选择:
#include <pthread.h>
void *RTPfun(void *client_addr);
int main(int argc, char *argv[])
{
char* client_addr;
pthread_t RTPthread;
...
...
pthread_create(&RTPthread, NULL, &RTPfun, client_addr)
}
void *RTPfun(void* data)
{
char *client_addr = (char*)data;
....
return;
}
You have to convert your char pointer to a void one. 您必须将char指针转换为void指针。
#include <pthread.h>
void *RTPfun(void *client_addr);
int main(int argc, char *argv[])
{
char* client_addr;
pthread_t RTPthread;
...
...
pthread_create(&RTPthread, NULL, &RTPfun, (void*)client_addr)
}
void *RTPfun(void * client_addr)
{
char *something = (char*)client_addr;
....
return;
}
Void pointers are used every time you need to pass some data and you cannot know in advance the type of variable (char*, integer*...) it will be. 每次需要传递一些数据时都会使用Void指针,而事先无法知道变量的类型(char *,integer * ...)。 The function you give to pthread_create
takes a void* as input, so you can cast your char pointer to a void one, and do the opposite in RTPfun. 您为pthread_create
提供的函数将void *作为输入,因此您可以将char指针转换为void指针,并在RTPfun中执行相反的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.