您的位置:首页 > 其它

关于线程创建的问题

2007-11-09 21:23 344 查看
#include <stdio.h>
#include <pthread.h>

#include<stdlib.h>

#include<sys/types.h>

#include<unistd.h>

void cout(char *s)
{
printf("%s /n",s);
}

int main()

{

pthread_t id;
int i,ret;
char *s="I love China!";

ret=pthread_create(&id,NULL,(void *)cout,s);

if(ret!=0){

printf ("Create pthread error!/n");

exit (1);

}

for(i=0;i<3;i++)
{
sleep(2);
printf("This is the main process./n");
printf("The thread_PID is :%u/n",id);
pthread_join(id,NULL);
}

return (0);

}
对于这段程序来讲,如果读者感兴趣的话,将char *s有键盘输入,看看会发生什么情况?

对于这个简单的线程例子,需要注意以下几个问题,首先是线程号ptread_t是unsigned long int 类型,而进程号是int类型。其次,在对线程创建函数来讲,尤其是对自定义函数中包括多个参数的函数来讲,在创建函数的第四个参数传递是一定是指针类型。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐