努力,一定可以成长起来

About pthread

看到很多程序都用了线程的概念,不知如何用,查了一下,还是不知所以然,先把大概的用法总结一下吧,慢慢理解 

1. 包含头文件  #include   <thread.h>  

2. 编译时加编译选项-lpthread
如:
      gcc -o program_name xxx.c -lpthread

给个例子先:

#include <pthread.h>

pthread_key_t key;

void echomsg(int t)
{
   printf("线程退出.\n");
   printf("destructor executed in thread %d, param=%d\n",pthread_self(),t);
}
void *child(void *arg)    //为每一个现成单独建一个执行函数
{
   return NULL;
 }

int main()
{
   int tid1;   //线程号
   printf("hello\n");
   pthread_key_create(&key,echomsg);   //注意在创建线程前创建私有数据,线程退出时会将key作为参数送入echomsg
   pthread_create(&tid1,NULL,child,NULL);   //创建线程,其中第一个NULL是设置线程属性,第二个NULL是child1函数的输入参数,线程的作用就是执行child1
   sleep(10);
   pthread_key_delete(key);   //释放TSD
   printf("main thread exit\n");
 
   retrun 0;
}

posted on 2007-10-31 15:21 努力的猪 阅读(350) 评论(2)  编辑 收藏 引用 所属分类: 学习

评论

# re: About pthread 2007-11-01 13:56 Minidx全文检索

不知所以然,是从什么地方,不能理解?  回复  更多评论   

# re: About pthread 2007-11-17 18:25 努力的猪

就是不太懂,不知道线程的作用和真正用法,以前没有用线程写过程序,呵呵,基础太差了 :(  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2007年11月>
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

导航

统计

常用链接

留言簿(1)

随笔分类

随笔档案

文章档案

搜索

最新评论

阅读排行榜

评论排行榜