潍坊网站建设SEO优化熊掌号,wangz网站建设,怎么上百度搜索,退役厅门户网站建设中标公告一、线程基本概念
1、概述
线程是允许应用程序并发的一种机制。线程共享进程内的所有资源。
线程是调度的基本单位。
每个线程都有自己的 errno。
所有 pthread 函数均以返回 0 表示成功#xff0c;返回一个正值表示失败。
编译 pthread 程序需要添加链接库#xff08;…一、线程基本概念
1、概述
线程是允许应用程序并发的一种机制。线程共享进程内的所有资源。
线程是调度的基本单位。
每个线程都有自己的 errno。
所有 pthread 函数均以返回 0 表示成功返回一个正值表示失败。
编译 pthread 程序需要添加链接库-lpthread。
线程的主要优势在于能够通过全局变量来共享信息。同时也引入一个问题多个线程对临界资源的竞争。
2、线程终止方式
1、线程函数执行 return 语句并返回指定值。
2、线程调用 pthread_exit。
3、调用 pthread_cancel() 取消线程。
4、任意线程调用 exit(), 或者主线程执行 return 语句。
二、Pthreads 数据类型 三、线程接口
1、创建线程
#include pthread.hint pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);pthread_create() 创建线程通过调用带有 arg 参数的 start_routine 函数开始执行。
2、终止线程
#include pthread.hvoid pthread_exit(void *retval);3、线程 ID
#include pthread.h// 获取当前线程 ID
pthread_t pthread_self(void);// 判断 2 个线程ID是否相等
int pthread_equal(pthread_t t1, pthread_t t2);4、连接已终止的线程
#include pthread.hint pthread_join(pthread_t thread, void **retval);pthread_join 等待由 thread 标识的线程终止。
如果 pthread_join 传入一个之前已经连接过的线程 ID将导致无法预知的行为。
默认情况下线程是可连接的(join)也就是程序退出时其他线程可以通过调用 pthread_join() 获取其返回状态。
5、线程分离
#include pthread.hint pthread_detach(pthread_t thread);有时我们不关心程序的返回状态只是希望系统在线程终止时能够自动清理并移除。在这种情况下可以调用 pthread_detach 并向 thread 参数传入指定线程的标识符将该线程标记为处于分离状态。
线程可以通过调用 pthread_detach() 实现自行分离。
一旦线程处于分离状态就不能再使用 pthread_join() 获取其状态也无法使其重返可连接状态。
6、线程取消
#include pthread.hint pthread_cancel(pthread_t thread);7、线程可取消性检查
#include pthread.hvoid pthread_testcancel(void);8、清理函数
#include pthread.hvoid pthread_cleanup_push(void (*routine)(void *),void *arg);
void pthread_cleanup_pop(int execute);9、向线程发送信号
#include signal.hint pthread_kill(pthread_t thread, int sig);#include signal.h
#include pthread.hint pthread_sigqueue(pthread_t thread, int sig,const union sigval value);10、操作线程信号掩码
#include signal.hint pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset);四、线程属性
五、线程同步 - 保护共享变量的访问互斥量
1、分配互斥量
1、静态分配
pthread_mutex_t mutex;2、动态分配
#include pthread.hint pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr);
pthread_mutex_t mutex PTHREAD_MUTEX_INITIALIZER;2、销毁互斥量
#include pthread.hint pthread_mutex_destroy(pthread_mutex_t *mutex);3、加锁和解锁互斥量
#include pthread.hint pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);调用 pthread_mutex_lock() 锁定互斥量。如果互斥量当前处于未锁定状态该调用将锁定互斥量并立即返回。如果互斥量处于锁定状态pthread_mutex_lock() 调用会一直阻塞直到该互斥量被解锁。
pthread_mutex_unlock() 解锁调用线程锁定的互斥量以下行为均为错误
1、对未锁定的互斥量进行解锁。
2、解锁其他线程锁定的互斥量。
4、互斥量死锁
5、互斥量属性
6、互斥量类型
1、PTHREAD_MUTEX_NORMAL
不具备死锁自检功能。
线程试图对已由自己锁定的互斥量加锁则发生死锁。
互斥量处于未锁定状态或由其他线程锁定对其解锁会导致不确定的结果。
2、PTHREAD_MUTEX_ERRORCHECK
此类互斥量的所有操作都会执行错误检查。
可以作为调试工具以发现程序在哪里违反了互斥量使用的基本原则。
3、PTHREAD_MUTEX_RECURSIVE
该互斥量维护一个锁计数器。当线程第一次取得互斥量时会将锁定计数器置1后续同一线程的每次加锁操作会递增锁定计数器的数值而解锁操作则会递减计数器计数只有锁计数器值降至0时才会释放。
六、线程同步 - 通知状态的改变条件变量
条件变量允许一个线程就某个共享变量(或其他共享资源)的状态变化通知其他线程。
条件变量总是结合互斥量使用。
所有线程都应该处理虚假的唤醒。
条件变量并不保存状态信息只是传递应用程序状态信息的一种通讯机制。发送信号时若无任何线程在等待条件变量这个信号也就不了了之。
1、分配条件变量
1、静态分配
pthread_cond_t cond;2、动态分配
#include pthread.hint pthread_cond_init(pthread_cond_t *restrict cond,const pthread_condattr_t *restrict attr);
pthread_cond_t cond PTHREAD_COND_INITIALIZER;2、销毁条件变量
#include pthread.hint pthread_cond_destroy(pthread_cond_t *cond);3、通知条件变量
#include pthread.h// 至少唤起一个线程
int pthread_cond_signal(pthread_cond_t *cond);
// 唤起所有阻塞线程
int pthread_cond_broadcast(pthread_cond_t *cond);4、等待条件变量
#include pthread.hint pthread_cond_timedwait(pthread_cond_t *restrict cond,pthread_mutex_t *restrict mutex,const struct timespec *restrict abstime);int pthread_cond_wait(pthread_cond_t *restrict cond,pthread_mutex_t *restrict mutex);七、线程安全
1、可重入性
要诀避免使用全局变量和静态变量。
2、一次性初始化
#include pthread.hint pthread_once(pthread_once_t *once_control,void (*init_routine)(void));
pthread_once_t once_control PTHREAD_ONCE_INIT;3、线程特有数据
4、线程局部存储
附录一多线程示例
1、main.c
#include stdio.h
#include pthread.h
#include unistd.hvoid* pthread_message_function(void *ptr);int main(int argc, char *argv[])
{pthread_t pthreadID1,pthreadID2;int ret 0;char *message1 thread1;char *message2 thread2;/* 1、创建线程1 */ret pthread_create(pthreadID1,NULL,pthread_message_function,(void*)message1);if(ret ! 0){printf(%s create fail!\r\n,message1);}else{printf(%s create sucess!\r\n,message1);}/* 2、创建线程2 */ret pthread_create(pthreadID2,NULL,pthread_message_function,(void*)message2);if(ret ! 0){printf(%s create fail!\r\n,message2);}else{printf(%s create sucess!\r\n,message2);}/* 3、休眠一定时间等待子线程结束 */sleep(10);printf(main thread exit\r\n);return 0;
}void* pthread_message_function(void *ptr)
{int i 0;/* 分离线程 - 避免僵尸线程产生 */pthread_detach(pthread_self());/* 业务逻辑 */for (i; i5; i) {printf(%s thread: %d\n, (char *)ptr, i);sleep(1);}
}2、makefile
a.out: main.cgcc main.c -o a.out -lpthread.PHONY : clean
clean:rm -rf a.out3、测试
[rootlocalhost pthread]# make
gcc main.c -o a.out -lpthread
[rootlocalhost pthread]# ls
a.out main.c makefile
[rootlocalhost pthread]# ./a.out
thread1 create sucess!
thread2 create sucess!
thread2 thread: 0
thread1 thread: 0
thread2 thread: 1
thread1 thread: 1
thread2 thread: 2
thread1 thread: 2
thread2 thread: 3
thread1 thread: 3
thread2 thread: 4
thread1 thread: 4
main thread exit
[rootlocalhost pthread]#