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

本文介绍了Linux线程的基础知识,包括线程数据类型如pthread_t、pthread_mutex_t等,以及线程函数如pthread_create、pthread_join等。同时,还讨论了线程的状态,如就绪、阻塞和运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
(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); - 允许一个线程获得它自己得标识符。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Suprman

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值