一个网站上线需要什么,淘宝网站制作多少钱,郑州天道做网站,wordpress captcha文章目录 3.8 线程同步3.9互斥锁互斥量互斥量相关操作函数 3.10死锁3.11读写锁读写锁读写锁相关操作函数 3.8 线程同步
问题#xff1a;卖第0张、第-1张门票#xff0c;三个线程买同一张门票等等。 原因#xff1a;三个线程并发执行去抢占线程资源#xff0c;A进来休眠600… 文章目录 3.8 线程同步3.9互斥锁互斥量互斥量相关操作函数 3.10死锁3.11读写锁读写锁读写锁相关操作函数 3.8 线程同步
问题卖第0张、第-1张门票三个线程买同一张门票等等。 原因三个线程并发执行去抢占线程资源A进来休眠6000微秒的时候B、C也可能进来。多个线程同时处理一个共享资源出现线程同步问题操作必须是原子性的。
/*使用多线程实现买票的案例。有3个窗口一共是100张票。
*/#include stdio.h
#include pthread.h
#include unistd.h// 全局变量所有的线程都共享这一份资源。
int tickets 100;//回调函数函数指针
void * sellticket(void * arg) {// 卖票while(tickets 0) {usleep(6000);//睡眠6000微秒printf(%ld 正在卖第 %d 张门票\n, pthread_self(), tickets);tickets--;}return NULL;
}int main() {// 创建3个子线程,子线程做同样的事情主线程不做买票操作只做回收子线程资源等pthread_t tid1, tid2, tid3;pthread_create(tid1, NULL, sellticket, NULL);pthread_create(tid2, NULL, sellticket, NULL);pthread_create(tid3, NULL, sellticket, NULL);// 回收子线程的资源,阻塞连接pthread_join(tid1, NULL);pthread_join(tid2, NULL);pthread_join(tid3, NULL);// 设置线程分离。// pthread_detach(tid1);// pthread_detach(tid2);// pthread_detach(tid3);pthread_exit(NULL); // 退出主线程return 0;
}
显示错误结果 倒数第四行终端 - 中断
线程同步会带来一定的效率问题但是是必要的。
3.9互斥锁
互斥量 互斥量相关操作函数 restrict : C语言的修饰符被修饰的指针不能由另外的一个指针进行操作。 pthread mutex t *restrict mutex xxx pthread mutex t * mutex1 mutex 是不可以通过mutex1去操作xxx的
#include stdio.h
#include pthread.h
#include unistd.h// 全局变量所有的线程都共享这一份资源。
int tickets 100;// 创建一个互斥量
pthread_mutex_t mutex;void * sellticket(void * arg) {// 卖票while(1) {// 加锁pthread_mutex_lock(mutex);if(tickets 0) {usleep(6000);printf(%ld 正在卖第 %d 张门票\n, pthread_self(), tickets);tickets--;}else {// 解锁pthread_mutex_unlock(mutex);break;}// 解锁pthread_mutex_unlock(mutex);}return NULL;
}int main() {// 初始化互斥量pthread_mutex_init(mutex, NULL);// 创建3个子线程pthread_t tid1, tid2, tid3;pthread_create(tid1, NULL, sellticket, NULL);pthread_create(tid2, NULL, sellticket, NULL);pthread_create(tid3, NULL, sellticket, NULL);// 回收子线程的资源,阻塞pthread_join(tid1, NULL);pthread_join(tid2, NULL);pthread_join(tid3, NULL);pthread_exit(NULL); // 退出主线程// 释放互斥量资源pthread_mutex_destroy(mutex);return 0;
} 3.10死锁
重复枷锁的一种可能
void B(){lock();
}
void A(){lock();B();
}#include stdio.h
#include pthread.h
#include unistd.h// 创建2个互斥量
pthread_mutex_t mutex1, mutex2;void * workA(void * arg) {pthread_mutex_lock(mutex1);sleep(1);pthread_mutex_lock(mutex2);printf(workA....\n);//先解2锁再解1锁pthread_mutex_unlock(mutex2);pthread_mutex_unlock(mutex1);return NULL;
}void * workB(void * arg) {pthread_mutex_lock(mutex2);sleep(1);pthread_mutex_lock(mutex1);printf(workB....\n);pthread_mutex_unlock(mutex1);pthread_mutex_unlock(mutex2);return NULL;
}int main() {// 初始化互斥量pthread_mutex_init(mutex1, NULL);pthread_mutex_init(mutex2, NULL);// 创建2个子线程pthread_t tid1, tid2;pthread_create(tid1, NULL, workA, NULL);pthread_create(tid2, NULL, workB, NULL);// 回收子线程资源pthread_join(tid1, NULL);pthread_join(tid2, NULL);// 释放互斥量资源pthread_mutex_destroy(mutex1);pthread_mutex_destroy(mutex2);return 0;
}
上述代码产生死锁线程1获得锁1且申请锁2线程2获得锁2且申请锁一。 产生死锁的四个必要条件 互斥条件、占有且等待条件、不可抢占条件、循环等待条件。
3.11读写锁
读写锁
读写锁比互斥锁效率要高一点读的时候是并发执行而互斥锁是串行
读写锁相关操作函数 #include stdio.h
#include pthread.h
#include unistd.h// 创建一个共享数据
int num 1;
// pthread_mutex_t mutex;
pthread_rwlock_t rwlock;void * writeNum(void * arg) {while(1) {pthread_rwlock_wrlock(rwlock);num;printf(write, tid : %ld, num : %d\n, pthread_self(), num);pthread_rwlock_unlock(rwlock);usleep(100);}return NULL;
}void * readNum(void * arg) {while(1) {pthread_rwlock_rdlock(rwlock);printf(read, tid : %ld, num : %d\n, pthread_self(), num);pthread_rwlock_unlock(rwlock);usleep(100);}return NULL;
}int main() {pthread_rwlock_init(rwlock, NULL);// 创建3个写线程5个读线程pthread_t wtids[3], rtids[5];for(int i 0; i 3; i) {pthread_create(wtids[i], NULL, writeNum, NULL);}for(int i 0; i 5; i) {pthread_create(rtids[i], NULL, readNum, NULL);}// 设置线程分离for(int i 0; i 3; i) {pthread_detach(wtids[i]);}for(int i 0; i 5; i) {pthread_detach(rtids[i]);}//若不加该语句return 0的话主线程的退出会影响子线程的执行//加上该语句后主线程的退出不影响子线程的执行pthread_exit(NULL);pthread_rwlock_destroy(rwlock);return 0;
}