CTDP linux 程序员手册 C和C++编程(9)Linux 线程

来源:互联网 发布:淘宝定时上新怎么更改 编辑:程序博客网 时间:2024/04/28 16:57
 
(9)Linux 线程
为了激活线程,一个数据结构需要被初始化。
线程数据类型
  • pthread_t
  • pthread_mutex_t – 互斥
  • pthread_cond_t – 条件量
  • pthread_key_t – 相关线程数据的处理键值
  • pthread_attr_t – 线程的属性
  • pthread_mutexattr_t – 互斥的属性
  • pthread_condattr_t – 条件量的属性
  • pthread_once_t – 一次初始化
线程结构和线程函数
1.       pthread_t
2.       为线程标识符,调用成功后返回。第三个参数是线程函数的地址。int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void *(*start)(void *), void *arg); - thread
3.       pthread_t pthread_self (void);
4.       当线程退出时,它所拥有的系统资源将被释放。int pthread_detach (pthread_t thread); -
5.       阻塞直到指定得线程中断。中断线程的返回值是可选得。int pthread_join (pthread_t thread, void **value_ptr); -
6.       int pthread_exit (void *value_ptr);
7.       如果两个线程相同则返回非0值,否则返回0值。int pthread_equal (pthread_t thr1, pthread_t thr2); -
8.      pthread_t pthread_self (void); - 允许一个线程获得它自己得标识符。

<SCRIPT type=text/javascript><!--google_ad_client = "pub-0057014352875780";google_ad_width = 120;google_ad_height = 600;google_ad_format = "120x600_as";google_ad_channel ="2584929872";google_color_border = ["336699","000000","B4D0DC","A8DDA0"];google_color_bg = ["FFFFFF","F0F0F0","ECF8FF","EBFFED"];google_color_link = ["0000FF","0000FF","0000CC","0000CC"];google_color_url = "008000";google_color_text = ["000000","000000","6F6F6F","6F6F6F"];//--> </SCRIPT>

线程状态
1.       在系统调度表里准备运行ready –
2.       等待一个互斥或资源blocked –
3.       系统调度执行running –
terminated – 线程一般情况退出或调用 Pthread_exit 退出. 它的资源不被释放,如果它是脱离得或者被主线程回收,资源将被释放。