Chinaunix首页 | 论坛 | 博客
  • 博客访问: 468111
  • 博文数量: 112
  • 博客积分: 2436
  • 博客等级: 大尉
  • 技术积分: 2769
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-04 19:48
文章分类

全部博文(112)

文章存档

2013年(7)

2012年(105)

分类: LINUX

2012-07-30 17:28:11

1、线程的创建
     int pthread_create(pthread_t *restrict thread,
                        const pthread_attr_t *restrict attr,
                        void *(*start_routine)(void*), void *restrict arg);
     thread用来存放新创建线程的ID
     attr指定新创建线程的属性
     start_routine是新创建线程要执行的函数
     arg是start_routine的唯一参数
   Note:
     #1 start_routine返回时,隐式调用pthread_exit()
           main返回时,隐式调用的是exit()
     #2 attr为NULL时,为默认属性:进程范围、非分离、缺省栈和栈大小
  返回值:调用成功返回0,否则出错
 
2、线程终止
  #1 单个线程的3种退出方式:
   ##1 从启动历程中返回,返回值是线程的退出码
   ##2 被同一进程中的其他进程取消pthread_cancle()
   ##3 线程自己调用pthread_exit()
  #2 其他线程可调用pthread_join()等待指定线程退出
  #3 主线程(main函数)若先于其他线程终止,需要调用pthread_exit()以线程方式终止,而不能调用exit()/_exit(),也不能return。
  Note:
   #1 若main()函数返回,则导致进程终止,相当于调用exit(),进程内所有的线程都将终止
   #2 若线程的start_routine()返回,则导致该线程终止,相当于调用了pthread_exit()
  
3、线程退出处理过程
 #1 返回值可被调用pthread_join()的线程得到
 #2 取消清理函数将被按照注册相反顺序调用(只会调用哪些push进去,但是没被pop出来的)
 #3 线程的私有数据会被调用相应的析构函数清理
 #4 线程终止不会释放进程级的资源
 #5 线程终止时,不会调用进程级的清理函数(通过atexit()设置)

 ##1 pthread_exit() 调用线程自我终止
 ##2 pthread_join() 等待其他线程终止
 ##3 pthread_cancle() 终止其他线程(取消类型:异步取消、延迟取消(取消点取消)。取消状态:enable、disable)
 ##4 pthread_cleanup_pop()与pthread_cleanup_push()
阅读(1435) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~