目录
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调试
掌握:临界资源(了解)、互斥机制(理解)、互斥锁(熟练)
1 线程通信 – 互斥
临界资源 一次只允许一个任务(进程、线程)访问的共享资源
临界区 访问临界资源的代码
互斥机制 mutex互斥锁 任务访问临界资源前申请锁,访问完后释放锁
2 互斥锁初始化 – pthread_mutex_init
两种方法创建互斥锁,静态方式和动态方式
动态方式:
#include <pthread.h>int pthread_mutex_init(pthread_mutex_t *mutex,const pthread_mutexattr_t * attr);
- 成功时返回0,失败时返回错误码
- mutex 指向要初始化的互斥锁对象
- attr 互斥锁属性,NULL表示缺省属性
- man 函数出现 No manual entry for pthread_mutex_xxx -解决办法 apt-get install manpages-posix-dev
静态方式:
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
3 互斥锁销毁 pthread_mutex_destroy
int pthread_mutex_destroy(pthread_mutex_t *mutex)
4 申请锁 – pthread_mutex_lock
#include <pthread.h>int pthread_mutex_lock(pthread_mutex_t *mutex);int pthread_mutex_trylock(pthread_mutex_t *mutex)
- 成功时返回0,失败时返回错误码
- mutex 指向要初始化的互斥锁对象
- pthread_mutex_lock 如果无法获得锁,任务阻塞
- pthread_mutex_trylock 如果无法获得锁,返回EBUSY而不是挂起等待
5 释放锁 – pthread_mutex_unlock
#include <pthread.h>int pthread_mutex_unlock(pthread_mutex_t *mutex);
- 成功时返回0,失败时返回错误码
- mutex 指向要初始化的互斥锁对象
- 执行完临界区要及时释放锁
示例:两个线程同时写一个文件的现象。
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>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<strlen(str)){c = str[i];fputc(c,fp);usleep(10);i++;}i = 0;usleep(1);}pthread_exit("func2 return");
}void *func(void *arg)
{pthread_detach(pthread_self());int i =0;char c;printf("This is func1 thread\n");char str[]="You read func1 thread\n";while(1){while(i<strlen(str)){c = str[i];fputc(c,fp);usleep(10);i++;}i = 0;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,NULL);pthread_create(&tid2,NULL,func2,NULL);while(1){sleep(1);}
}//实验现象Yfuounc r2 ealid nefunI c1wr tithre eafud
nc2Yo uli nreeaId wfurincte1 tfuhrncea2 d
liYneouI r weardit efu fncu1nc t2 hrlieaned
IY wouri rteea fd unfuc2nc 1li tnehreI adw
riYteou f runeac2d lfuinnce1 I thwrreiatdefYunouc2 r leaidne fIu ncwr1 itthe refuadn
c2Y loiu nreeIad w friuntec1 futhncre2 adli
neYoIu wreriadte f fununc1c2 t lhrineaed
I Ywrouit re eafudn fc2un lc1in tehIre wadri
teYo fu unrec2ad l finunec1I twrhriteae d
fuYncou2 rlieaned Ifu wncri1 teth freunadc2lYoinu ereI adwr fitune c1fu tnchr2 ealidn
eYoI u wrreitade ffuunncc12 tlihrneeaIdwYrioute r feaund cf2 unlic1ne tIhr wearid
te Yofuun rc2ea ld infuencI1 wtrhirteea dfu
加上互斥锁的示例:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>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<strlen(str)){c = str[i];fputc(c,fp);usleep(10);i++;}pthread_mutex_unlock(&mutex); //解锁i = 0;usleep(1);}pthread_exit("func2 return");
}void *func(void *arg)
{pthread_detach(pthread_self());int i =0;char c;printf("This is func1 thread\n");char str[]="You read func1 thread\n";while(1){pthread_mutex_lock(&mutex);while(i<strlen(str)){c = str[i];fputc(c,fp);usleep(10);i++;}pthread_mutex_unlock(&mutex);i = 0;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,NULL);pthread_create(&tid2,NULL,func2,NULL);while(1){sleep(1);}
}//结果
I write func2 lineYou read func1 thread
I write func2 lineYou read func1 thread
I write func2 lineYou read func1 thread
I write func2 lineYou read func1 thread
I write func2 lineYou read func1 thread
I write func2 lineYou read func1 thread
I write func2 lineYou read func1 thread
I write func2 lineYou read func1 thread
I write func2 lineYou read func1 thread
6 读写锁
问:多个线程只是读文件,这时候不会造成文件写坏,加了互斥锁会出现什么问题?
如果加了互斥锁,读文件的效率很低,多个线程读文件是不影响的。
如果一个线程在写,多个线程在读,那么读到一半,文件被改了,那么会出现读错误。
读写锁必要性:提高线程执行效率
特性:
写者:写者使用写锁,如果当前没有读者,也没有其他写者,写者立即获得写锁;否则写者将等待,直到没有读者和写者。
读者:读者使用读锁,如果当前没有写者,读者立即获得读锁;否则读者等待,直到没有写者。
注意:
-同一时刻只有一个线程可以获得写锁,同一时刻可以有多个线程获得读锁。
-读写锁出于写锁状态时,所有试图对读写锁加锁的线程,不管是读者试图加读锁,还是写者试图加写锁,都会被阻塞。
-读写锁处于读锁状态时,有写者试图加写锁时,之后的其他线程的读锁请求会被阻塞,以避免写者长时间的不写锁
初始化一个读写锁 pthread_rwlock_init
读锁定读写锁 pthread_rwlock_rdlock
非阻塞读锁定 pthread_rwlock_tryrdlock
写锁定读写锁 pthread_rwlock_wrlock
非阻塞写锁定 pthread_rwlock_trywrlock
解锁读写锁 pthread_rwlock_unlock
释放读写锁 pthread_rwlock_destroy
示例:
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>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<strlen(str)){c = str[i];fputc(c,fp);usleep(1);i++;}pthread_rwlock_unlock(&rwlock);i=0;usleep(1);}pthread_exit("func2 exit");}void *func(void *arg){pthread_detach(pthread_self());printf("This is func1 thread\n");char str[]="You read func1 thread\n";char c;int i=0;while(1){pthread_rwlock_wrlock(&rwlock);while(i<strlen(str)){c = str[i];fputc(c,fp);i++;usleep(1);}pthread_rwlock_unlock(&rwlock);i=0;usleep(1);}pthread_exit("func1 exit");
}int main(){pthread_t tid1,tid2,tid3,tid4;void *retv;int i;fp = fopen("1.txt","a+");if(fp==NULL){perror("fopen");return 0;}pthread_rwlock_init(&rwlock,NULL);pthread_create(&tid1,NULL,read_func,1);pthread_create(&tid2,NULL,read_func,2);pthread_create(&tid3,NULL,func,NULL);pthread_create(&tid4,NULL,func2,NULL);while(1){ sleep(1);} }
2个读线程创建的快一点,如果没读完,写线程是写不进去的。如果先写,同样读也要等待。
7 死锁的避免
什么是死锁
一把锁是不会出现死锁的。一般两把以上才会出现死锁
示例:模式死锁
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>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 <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>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 <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>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 <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>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);}
}
8 条件变量(信号量)
应用场景:生产者消费者问题,是线程同步的一种手段。
必要性:为了实现等待某个资源,让线程休眠。提高运行效率
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 <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>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);
9 线程池概念和实现
9.1 概念
概念:
通俗的讲就是一个线程的池子,可以循环的完成任务的一组线程集合
必要性:
我们平时创建一个线程,完成某一个任务,等待线程的退出。但当需要创建大量的线程时,假设T1为创建线程时间,T2为在线程任务执行时间,T3为线程销毁时间,当 T1+T3 > T2,这时候就不划算了,使用线程池可以降低频繁创建和销毁线程所带来的开销,任务处理时间比较短的时候这个好处非常显著。
线程池的基本结构:
1 任务队列,存储需要处理的任务,由工作线程来处理这些任务
2 线程池工作线程,它是任务队列任务的消费者,等待新任务的信号
9.2 线程池的实现
创建线程池的基本结构:
任务队列链表 typedef struct Task;
线程池结构体 typedef struct ThreadPool;
线程池的初始化:
pool_init()
{创建一个线程池结构实现任务队列互斥锁和条件变量的初始化创建n个工作线程
}
线程池添加任务
pool_add_task
{判断是否有空闲的工作线程给任务队列添加一个节点给工作线程发送信号newtask
}
实现工作线程
workThread
{while(1){等待newtask任务信号从任务队列中删除节点执行任务}
}
线程池的销毁
pool_destory
{删除任务队列链表所有节点,释放空间删除所有的互斥锁条件变量删除线程池,释放空间
}
示例:
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>#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;i<POOL_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(){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个线程池,不让任务丢失。
9.3 练习
实现课程线程池代码
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>#define POOL_MAX_NUM 10typedef 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();}
10 线程的GDB调试
显示线程
info thread
切换线程
thread xxx
b 6 thread 3 //线程运行后给线程3第6行打端点
bt //可以打印出当前线程的函数调用栈信息。它会显示函数调用链的序列,从当前执行点一直追溯到代码的起始点,以帮助开发人员定位问题所在
GDB为特定线程设置断点
break location thread id
GDB设置线程锁
set scheduler-locking on/off //on:其他线程会暂停。可以单独调试一个线程
#include <pthread.h>
#include <stdio.h>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);}