目录
1 线程通信 – 互斥
2 互斥锁初始化 – pthread_mutex_init
3 互斥锁销毁 pthread_mutex_destroy
4 申请锁 – pthread_mutex_lock
5 释放锁 – pthread_mutex_unlock
6 读写锁
7 死锁的避免
8 条件变量(信号量)
9 线程池概念和实现
9.1 概念
9.2 线程池的实现
9.3 练习
10 线程的GDB调试
掌握:临界资源(了解)、互斥机制(理解)、互斥锁(熟练)
临界资源 一次只允许一个任务(进程、线程)访问的共享资源
临界区 访问临界资源的代码
互斥机制 mutex互斥锁 任务访问临界资源前申请锁,访问完后释放锁
两种方法创建互斥锁,静态方式和动态方式
动态方式:
#include
int pthread_mutex_init(pthread_mutex_t *mutex,
const pthread_mutexattr_t * attr);
静态方式:
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int pthread_mutex_destroy(pthread_mutex_t *mutex)
#include
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex)
#include
int pthread_mutex_unlock(pthread_mutex_t *mutex);
示例:两个线程同时写一个文件的现象。
#include
#include
#include
#include
FILE *fp;
void *func2(void *arg)
{
pthread_detach(pthread_self());
char c;
int i=0;
printf("This is func2 thread\n");
char str[]="I write func2 line";
while(1)
{
while(i
加上互斥锁的示例:
#include
#include
#include
#include
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; //初始化
FILE *fp;
void *func2(void *arg)
{
pthread_detach(pthread_self());
char c;
int i=0;
printf("This is func2 thread\n");
char str[]="I write func2 line";
while(1)
{
pthread_mutex_lock(&mutex); //加锁
while(i
问:多个线程只是读文件,这时候不会造成文件写坏,加了互斥锁会出现什么问题?
如果加了互斥锁,读文件的效率很低,多个线程读文件是不影响的。
如果一个线程在写,多个线程在读,那么读到一半,文件被改了,那么会出现读错误。
读写锁必要性:提高线程执行效率
特性:
写者:写者使用写锁,如果当前没有读者,也没有其他写者,写者立即获得写锁;否则写者将等待,直到没有读者和写者。
读者:读者使用读锁,如果当前没有写者,读者立即获得读锁;否则读者等待,直到没有写者。
注意:
-同一时刻只有一个线程可以获得写锁,同一时刻可以有多个线程获得读锁。
-读写锁出于写锁状态时,所有试图对读写锁加锁的线程,不管是读者试图加读锁,还是写者试图加写锁,都会被阻塞。
-读写锁处于读锁状态时,有写者试图加写锁时,之后的其他线程的读锁请求会被阻塞,以避免写者长时间的不写锁
初始化一个读写锁 pthread_rwlock_init
读锁定读写锁 pthread_rwlock_rdlock
非阻塞读锁定 pthread_rwlock_tryrdlock
写锁定读写锁 pthread_rwlock_wrlock
非阻塞写锁定 pthread_rwlock_trywrlock
解锁读写锁 pthread_rwlock_unlock
释放读写锁 pthread_rwlock_destroy
示例:
#include
#include
#include
#include
pthread_rwlock_t rwlock;
FILE *fp;
void * read_func(void *arg){
pthread_detach(pthread_self());
printf("read thread\n");
char buf[32]={0};
while(1){
//rewind(fp); //从线程开头读
pthread_rwlock_rdlock(&rwlock); //如果加了wrlock,那么线程1 读完才轮到线程2读
while(fgets(buf,32,fp)!=NULL){
printf("%d,rd=%s\n",(int)arg,buf);
usleep(1000);
}
pthread_rwlock_unlock(&rwlock);
sleep(1);
}
}
void *func2(void *arg){
pthread_detach(pthread_self());
printf("This func2 thread\n");
char str[]="I write func2 line\n";
char c;
int i=0;
while(1){
pthread_rwlock_wrlock(&rwlock);
while(i
2个读线程创建的快一点,如果没读完,写线程是写不进去的。如果先写,同样读也要等待。
什么是死锁
一把锁是不会出现死锁的。一般两把以上才会出现死锁
示例:模式死锁
#include
#include
#include
#include
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
FILE *fp;
void *func2(void *arg)
{
pthread_detach(pthread_self());
printf("This is func2 thread\n");
while(1)
{
pthread_mutex_lock(&mutex2);
printf("%d,I got lock2\n",(int)arg);
sleep(1);
pthread_mutex_lock(&mutex);
printf("%d,I got 2 locks\n",(int)arg);
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&mutex2);
usleep(1);
}
pthread_exit("func2 return");
}
void *func(void *arg)
{
pthread_detach(pthread_self());
printf("This is func1 thread\n");
while(1)
{
pthread_mutex_lock(&mutex);
printf("%d,I got lock1\n",(int)arg);
sleep(1);
pthread_mutex_lock(&mutex2);
printf("%d,I got 2 locks\n",(int)arg);
pthread_mutex_unlock(&mutex2);
pthread_mutex_unlock(&mutex);
usleep(1);
}
pthread_exit("func1 return");
}
int main(int argc,char * argv[])
{
pthread_t tid,tid2;
void *retv;
fp = fopen("1.txt","a+");
if(fp == NULL)
{
perror("fopen");
return 0;
}
pthread_create(&tid,NULL,func,1);
pthread_create(&tid2,NULL,func2,2);
while(1)
{
sleep(1);
}
}
//死锁结果
linux@linux:~/Desktop$ gcc -g -o mutex mutex.c -lpthread
mutex.c: In function ‘main’:
mutex.c:61:2: warning: passing argument 4 of ‘pthread_create’ makes pointer from integer without a cast [enabled by default]
pthread_create(&tid,NULL,func,1);
^
In file included from mutex.c:2:0:
/usr/include/pthread.h:244:12: note: expected ‘void * __restrict__’ but argument is of type ‘int’
extern int pthread_create (pthread_t *__restrict __newthread,
^
mutex.c:62:2: warning: passing argument 4 of ‘pthread_create’ makes pointer from integer without a cast [enabled by default]
pthread_create(&tid2,NULL,func2,2);
^
In file included from mutex.c:2:0:
/usr/include/pthread.h:244:12: note: expected ‘void * __restrict__’ but argument is of type ‘int’
extern int pthread_create (pthread_t *__restrict __newthread,
^
linux@linux:~/Desktop$ ./mutex
This is func2 thread
2,I got lock2
This is func1 thread
1,I got lock1
。。。
解决方法1:通过时间差让线程1 先执行,获取两把锁后再休息,线程2再执行,获取两把锁后再休息
#include
#include
#include
#include
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
FILE *fp;
void *func2(void *arg)
{
pthread_detach(pthread_self());
printf("This is func2 thread\n");
while(1)
{
pthread_mutex_lock(&mutex2);
printf("%d,I got lock2\n",(int)arg);
sleep(1);
pthread_mutex_lock(&mutex);
printf("%d,I got 2 locks\n",(int)arg);
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&mutex2);
sleep(5);
}
pthread_exit("func2 return");
}
void *func(void *arg)
{
pthread_detach(pthread_self());
printf("This is func1 thread\n");
while(1)
{
pthread_mutex_lock(&mutex);
printf("%d,I got lock1\n",(int)arg);
sleep(1);
pthread_mutex_lock(&mutex2);
printf("%d,I got 2 locks\n",(int)arg);
pthread_mutex_unlock(&mutex2);
pthread_mutex_unlock(&mutex);
sleep(5);
}
pthread_exit("func1 return");
}
int main(int argc,char * argv[])
{
pthread_t tid,tid2;
void *retv;
fp = fopen("1.txt","a+");
if(fp == NULL)
{
perror("fopen");
return 0;
}
pthread_create(&tid,NULL,func,1);
sleep(2);
pthread_create(&tid2,NULL,func2,2);
while(1)
{
sleep(1);
}
}
//执行结果
linux@linux:~/Desktop$ ./mutex
This is func1 thread
1,I got lock1
1,I got 2 locks
This is func2 thread
2,I got lock2
2,I got 2 locks
1,I got lock1
1,I got 2 locks
2,I got lock2
1,I got lock1
解决方法2:调整锁的顺序。都先获取锁1,再去获取锁2,不会同时造成2个资源被锁的情况。
#include
#include
#include
#include
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
FILE *fp;
void *func2(void *arg)
{
pthread_detach(pthread_self());
printf("This is func2 thread\n");
while(1)
{
pthread_mutex_lock(&mutex);
printf("%d,I got lock2\n",(int)arg);
sleep(1);
pthread_mutex_lock(&mutex2);
printf("%d,I got 2 locks\n",(int)arg);
pthread_mutex_unlock(&mutex2);
pthread_mutex_unlock(&mutex);
sleep(1);
}
pthread_exit("func2 return");
}
void *func(void *arg)
{
pthread_detach(pthread_self());
printf("This is func1 thread\n");
while(1)
{
pthread_mutex_lock(&mutex);
printf("%d,I got lock1\n",(int)arg);
sleep(1);
pthread_mutex_lock(&mutex2);
printf("%d,I got 2 locks\n",(int)arg);
pthread_mutex_unlock(&mutex2);
pthread_mutex_unlock(&mutex);
sleep(1);
}
pthread_exit("func1 return");
}
int main(int argc,char * argv[])
{
pthread_t tid,tid2;
void *retv;
fp = fopen("1.txt","a+");
if(fp == NULL)
{
perror("fopen");
return 0;
}
pthread_create(&tid,NULL,func,1);
pthread_create(&tid2,NULL,func2,2);
while(1)
{
sleep(1);
}
}
linux@linux:~/Desktop$ ./mutex
This is func2 thread
2,I got lock2
This is func1 thread
2,I got 2 locks
1,I got lock1
1,I got 2 locks
2,I got lock2
2,I got 2 locks
1,I got lock1
1,I got 2 locks
2,I got lock2
2,I got 2 locks
1,I got lock1
1,I got 2 locks
2,I got lock2
总结:
练习:实现多个线程写一个文件,使用互斥锁
#include
#include
#include
#include
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void *write_func1(void *arg)
{
pthread_detach(pthread_self());
printf("This is write_func1 thread\n");
while(1)
{
pthread_mutex_lock(&mutex);
printf("%d,I got lock\n",(int)arg);
pthread_mutex_unlock(&mutex);
sleep(1);
}
pthread_exit("write_func1 return");
}
void *write_func2(void *arg)
{
pthread_detach(pthread_self());
printf("This is write_func2 thread\n");
while(1)
{
pthread_mutex_lock(&mutex);
printf("%d,I got lock\n",(int)arg);
pthread_mutex_unlock(&mutex);
sleep(1);
}
pthread_exit("write_func2 return");
}
int main(int argc,char * argv[])
{
pthread_t tid,tid2;
void *retv;
pthread_create(&tid,NULL,write_func1,1);
pthread_create(&tid2,NULL,write_func2,2);
while(1)
{
sleep(1);
}
}
应用场景:生产者消费者问题,是线程同步的一种手段。
必要性:为了实现等待某个资源,让线程休眠。提高运行效率
pthread_cond_wait(&m_cond,&m_mutex); //完全阻塞等待
int pthread_cond_timedwait(pthread_cond_t *restrict cond, //超时等待
pthread_mutex_t *restrict mutex,
const struct timespec *restrict abstime);
int pthread_cond_signal(pthread_cond_t *cond); //通知1个线程
int pthread_cond_broadcast(pthread_cond_t *cond); //通知多个线程
使用方法:
静态初始化或使用动态初始化
pthread_cond_t cond = PTHREAD_COND_INITIALIZER; //静态初始化条件变量
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; //初始化互斥量
pthread_cond_t cond; //动态初始化条件变量
pthread_cond_init(&cond); //动态初始化条件变量
生产资源线程:
pthread_mutex_lock(&mutex);
开始产生资源
pthread_cond_sigal(&cond); //通知一个消费线程
或者
pthread_cond_broadcast(&cond); //广播通知多个消费线程
pthread_mutex_unlock(&mutex);
消费者线程:
pthread_mutex_lock(&mutex);
while (如果没有资源){ //防止惊群效应
pthread_cond_wait(&cond, &mutex);
}
有资源了,消费资源
pthread_mutex_unlock(&mutex);
示例:
#include
#include
#include
#include
pthread_cond_t hasTaxi=PTHREAD_COND_INITIALIZER;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
struct taxi{
struct taxi *next;
int num;
};
struct taxi *Head=NULL;
void *taxiarv(void *arg){
printf("taxi arrived thread\n");
pthread_detach(pthread_self());
struct taxi *tx;
int i=1;
while(1){
tx = malloc(sizeof(struct taxi));
tx->num = i++;
printf("taxi %d comming\n",tx->num);
pthread_mutex_lock(&lock);
tx->next = Head;
Head = tx;
pthread_cond_signal(&hasTaxi); //生产了一个资源信号
//pthread_cond_broadcast(&hasTaxi); //有可能产生段错误
pthread_mutex_unlock(&lock);
sleep(1);
}
pthread_exit(0);
}
void *takeTaxi(void *arg){
printf("take taxi thread\n");
pthread_detach(pthread_self());
struct taxi *tx;
while(1){
pthread_mutex_lock(&lock);
while(Head==NULL) //这句不能去
{
pthread_cond_wait(&hasTaxi,&lock);
}//有资源了可以消费
tx = Head;
Head=tx->next;
printf("%d,Take taxi %d\n",(int)arg,tx->num);
free(tx);
pthread_mutex_unlock(&lock);
}
pthread_exit(0);
}
int main(){
pthread_t tid1,tid2,tid3;
pthread_create(&tid1,NULL,taxiarv,NULL);
// sleep(5);
pthread_create(&tid2,NULL,takeTaxi,(void*)1); //(谁先获得信号谁执行,没有先后规律,并行。
pthread_create(&tid2,NULL,takeTaxi,(void*)2);
pthread_create(&tid2,NULL,takeTaxi,(void*)3);
while(1) {
sleep(1);
}
}
注意:
1 pthread_cond_wait(&cond, &mutex),在没有资源等待是是先unlock 休眠,等资源到了,再lock
所以pthread_cond_wait he pthread_mutex_lock 必须配对使用。
2 如果pthread_cond_signal或者pthread_cond_broadcast 早于 pthread_cond_wait ,则有可能会丢失信号。(对应代码中 while(Head==NULL) 不能去)
3 pthead_cond_broadcast 信号会被多个线程收到,这叫线程的惊群效应。所以需要加上判断条件while循环。(需要加上while(Head==NULL) 防止同时获取,空指针被获取)
练习:
条件变量有两种初始化的方式,写出这两种方式:
pthread_cond_t cond = PTHREAD_COND_INITIALIZER; //静态初始化条件变量
pthread_cond_t cond; //动态初始化条件变量
pthread_cond_init(&cond, NULL);
概念:
通俗的讲就是一个线程的池子,可以循环的完成任务的一组线程集合
必要性:
我们平时创建一个线程,完成某一个任务,等待线程的退出。但当需要创建大量的线程时,假设T1为创建线程时间,T2为在线程任务执行时间,T3为线程销毁时间,当 T1+T3 > T2,这时候就不划算了,使用线程池可以降低频繁创建和销毁线程所带来的开销,任务处理时间比较短的时候这个好处非常显著。
线程池的基本结构:
1 任务队列,存储需要处理的任务,由工作线程来处理这些任务
2 线程池工作线程,它是任务队列任务的消费者,等待新任务的信号
创建线程池的基本结构:
任务队列链表 typedef struct Task;
线程池结构体 typedef struct ThreadPool;
线程池的初始化:
pool_init()
{
创建一个线程池结构
实现任务队列互斥锁和条件变量的初始化
创建n个工作线程
}
线程池添加任务
pool_add_task
{
判断是否有空闲的工作线程
给任务队列添加一个节点
给工作线程发送信号newtask
}
实现工作线程
workThread
{
while(1)
{
等待newtask任务信号
从任务队列中删除节点
执行任务
}
}
线程池的销毁
pool_destory
{
删除任务队列链表所有节点,释放空间
删除所有的互斥锁条件变量
删除线程池,释放空间
}
示例:
#include
#include
#include
#include
#define POOL_NUM 10
typedef struct Task{
void *(*func)(void *arg);
void *arg;
struct Task *next;
}Task;
typedef struct ThreadPool{
pthread_mutex_t taskLock;
pthread_cond_t newTask;
pthread_t tid[POOL_NUM];
Task *queue_head;
int busywork;
}ThreadPool;
ThreadPool *pool;
void *workThread(void *arg){
while(1){
pthread_mutex_lock(&pool->taskLock);
pthread_cond_wait(&pool->newTask,&pool->taskLock);
Task *ptask = pool->queue_head;
pool->queue_head = pool->queue_head->next;
pthread_mutex_unlock(&pool->taskLock);
ptask->func(ptask->arg);
pool->busywork--;
}
}
void *realwork(void *arg){
printf("Finish work %d\n",(int)arg);
}
void pool_add_task(int arg){
Task *newTask;
pthread_mutex_lock(&pool->taskLock);
while(pool->busywork>=POOL_NUM){
pthread_mutex_unlock(&pool->taskLock); //休眠时候释放锁
usleep(10000); //线程池满等待
pthread_mutex_lock(&pool->taskLock); //休眠结束再锁,否则别人访问不到资源
}
pthread_mutex_unlock(&pool->taskLock);
newTask = malloc(sizeof(Task));
newTask->func = realwork;
newTask->arg = arg;
pthread_mutex_lock(&pool->taskLock); //操作队列需要加锁
Task *member = pool->queue_head;
if(member==NULL){
pool->queue_head = newTask;
}else{
while(member->next!=NULL){ //新任务插入队列尾部
member=member->next;
}
member->next = newTask;
}
pool->busywork++;
pthread_cond_signal(&pool->newTask);
pthread_mutex_unlock(&pool->taskLock);
}
void pool_init(){
pool = malloc(sizeof(ThreadPool));
pthread_mutex_init(&pool->taskLock,NULL);
pthread_cond_init(&pool->newTask,NULL);
pool->queue_head = NULL;
pool->busywork=0;
for(int i=0;itid[i],NULL,workThread,NULL);
}
}
void pool_destory(){
Task *head;
while(pool->queue_head!=NULL){
head = pool->queue_head;
pool->queue_head = pool->queue_head->next;
free(head);
}
pthread_mutex_destroy(&pool->taskLock);
pthread_cond_destroy(&pool->newTask);
free(pool);
}
int main(){
pool_init();
sleep(20);
for(int i=1;i<=20;i++){
pool_add_task(i);
}
sleep(5);
pool_destory();
}
编译错误:
error: ‘ThreadPool {aka struct ThreadPool}’ has no member named ‘head’
意义:ThreadPool 结构体没有head这个成员。
解决:检查是否拼写错误。
error: too few arguments to function ‘pthread_mutex_init’
意思:pthread_mutex_init这个函数参数少了
解决:检查函数的参数,添加对应的参数
运行结果:20个任务共享10个线程池,不让任务丢失。
实现课程线程池代码
#include
#include
#include
#define POOL_MAX_NUM 10
typedef struct _Task
{
void *(*func)(void *arg);
void *arg;
struct _Task * next;
}Task;
typedef struct _ThreadPool
{
pthread_mutex_t taskLock;
pthread_cond_t newTask;
pthread_t tid[POOL_MAX_NUM];
Task *queue_head;
int busywork;
}ThreadPool;
ThreadPool *pool;
void *realwork(void *arg)
{
printf("Finish work %d\n",(int)arg);
}
void pool_add_task(int arg)
{
Task *newTask;
pthread_mutex_lock(&pool->taskLock);
while(pool->busywork >= POOL_MAX_NUM)
{
pthread_mutex_unlock(&pool->taskLock);
usleep(10000);
pthread_mutex_lock(&pool->taskLock);
}
pthread_mutex_unlock(&pool->taskLock);
newTask = malloc(sizeof(Task));
newTask->func = realwork;
newTask->arg = &arg;
pthread_mutex_lock(&pool->taskLock);
Task * member = pool->queue_head;
if(member == NULL)
{
pool->queue_head = newTask;
}
else
{
while(member->next != NULL)
{
member = member->next;
}
member->next = newTask;
}
pool->busywork ++;
pthread_cond_signal(&pool->newTask);
pthread_mutex_unlock(&pool->taskLock);
}
void *workThread(void *arg)
{
while(1)
{
pthread_mutex_lock(&pool->taskLock);
//等待newtask任务信号
pthread_cond_wait(&pool->newTask,&pool->taskLock);
//从队列中删除一个节点
Task *ptask = pool->queue_head;
pool->queue_head = pool->queue_head->next;
pthread_mutex_unlock(&pool->taskLock);
//执行任务
ptask->func(ptask->arg);
pool->busywork--;
}
}
void pool_init()
{
int i;
pool = malloc(sizeof(ThreadPool));
pthread_mutex_init(&pool->taskLock,NULL);
pthread_cond_init(&pool->newTask,NULL);
pool->queue_head = NULL;
pool->busywork = 0;
for(i = 0; i < POOL_MAX_NUM; i++)
{
pthread_create(&pool->tid[i],NULL,workThread,NULL);
}
}
void pool_destory(){
Task *head;
while(pool->queue_head!=NULL){
head = pool->queue_head;
pool->queue_head = pool->queue_head->next;
free(head);
}
pthread_mutex_destroy(&pool->taskLock);
pthread_cond_destroy(&pool->newTask);
free(pool);
}
int main(int argc,char *argv[])
{
int i;
pool_init();
sleep(5);
for(i = 1; i <= 30; i++)
{
pool_add_task(i);
}
sleep(5);
pool_destory();
}
显示线程
info thread
切换线程
thread xxx
b 6 thread 3 //线程运行后给线程3第6行打端点
bt //可以打印出当前线程的函数调用栈信息。它会显示函数调用链的序列,从当前执行点一直追溯到代码的起始点,以帮助开发人员定位问题所在
GDB为特定线程设置断点
break location thread id
GDB设置线程锁
set scheduler-locking on/off //on:其他线程会暂停。可以单独调试一个线程
#include
#include
void *testThread(void *arg){
char *threadName = (char*)arg;
printf("Current running %s\n",threadName);
printf("aaaaaaaa\n");
printf("bbbbbbbb\n");
pthread_exit(0);
}
int main(){
pthread_t tid1,tid2;
pthread_create(&tid1,NULL,testThread,"thread1");
pthread_create(&tid2,NULL,testThread,"thread2");
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
}