创建线程函数pthread_create,在c/c++的用法。

winter_sui 2004-09-08 03:34:09
#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++编译就有问题。
...全文
1431 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
winter_sui 2004-09-10
  • 打赏
  • 举报
回复
结帖!
顺便说一下,引用别人的话。
c++比c的语法严格,就thread函数来说,c++中要ret=pthread_create(&id,NULL,(void *(*)(void)thread),NULL);


>>顺便问一下,什么时候使用pthread_join和pthread_detach,有什么区别
我不会解决不了。

sharp3000 2004-09-09
  • 打赏
  • 举报
回复
楼上的说得很对。函数名改为 void* thread(void *p)
kandyivy 2004-09-09
  • 打赏
  • 举报
回复
void thread() ==> void* thread(void *p)
lovepeacer 2004-09-09
  • 打赏
  • 举报
回复
顺便问一下,什么时候使用pthread_join和pthread_detach,有什么区别
blh 2004-09-08
  • 打赏
  • 举报
回复
void thread() ==> void* thread(void *p)
winter_sui 2004-09-08
  • 打赏
  • 举报
回复
thread.cpp: In function `int main()':
thread.cpp:16: invalid conversion from `void*' to `void*(*)(void*)'
pacman2000 2004-09-08
  • 打赏
  • 举报
回复
报什么错?
Xeroo 2004-09-08
  • 打赏
  • 举报
回复
编译错误还是连接错误还是运行错误?

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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