pthread_create函数的使用。。

winter_sui 2004-09-08 03:43:32
#include <stdio.h>
#include <pthread.h>
void thread()
{
int i;
for(i=0;i<3;i++)
printf("This is a pthread.\n");
return ;
}

int main(void)
{
pthread_t id;
int i,ret;

ret=pthread_create(&id,NULL,(void *)thread,NULL);
if(ret!=0){
printf ("Create pthread error!\n");
return 1;
}
for(i=0;i<3;i++)
printf("This is the main process.\n");

return (0);
}

同样的程序,用gcc编译没问题。为什么用g++编译就有问题。
...全文
618 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
winter_sui 2004-09-08
  • 打赏
  • 举报
回复
to: whyglinux(山青水秀)
太帅了,偶像签个名吧。
留下mail吧,以后俺好找你。
whyglinux 2004-09-08
  • 打赏
  • 举报
回复
C中对类型的检查不是太严格,而在C++中严格检查类型,类型不对是编译不过的。

将 pthread_create 函数的第三个实参改为 (void*(*)(void*))thread,即将函数名 thread 强制转换为所需的函数指针就可以了。
cenlmmx 2004-09-08
  • 打赏
  • 举报
回复
加上
pthread_join(id,NULL);
pthread_cancel(id);

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧