无法正确创建线程 [英] can't create thread properly

查看:223
本文介绍了无法正确创建线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我我做错了什么?
(我省略了程序的其余部分,因为它很长...)

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;
}

错误:

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 *)’


推荐答案

你必须将char指针转换为void。

You have to convert your char pointer to a void one.

#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;
}

每次需要传递一些数据时都会使用虚指针,提前的变量类型(char *,integer * ...)就会是。你给 pthread_create 的函数输入一个void *作为输入,所以你可以将char指针转换为一个void指针,并在RTPfun中做相反的操作。

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. 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.

这篇关于无法正确创建线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆