[Linux]多线程同步之sem_wait()学习笔记
来源:互联网 发布:央视网商城软件 编辑:程序博客网 时间:2024/06/06 03:55
1、semaphore 的这种信号量不仅可用于同一进程的线程同步,也可以用于不同进程间同步。
一个生产者-消费者例子:生产者不停的向一个固定大小的环形队列中添加数据,消费者从环形队列中清零数据,如果生产者积累的数据大于环形队列长度,则等待消费者清除数据有空位后再生产。
#include <stdlib.h>#include <pthread.h>#include <stdio.h>#include <semaphore.h>#include <unistd.h>#define NUM 5int queue[NUM];sem_t blank_number, product_number;void *producer(void *arg){ int p = 0; while(1){ sem_wait(&blank_number); queue[p] = rand() % 1000 + 1; printf("produce %d\n", queue[p]); p = (p + 1) % NUM; sem_post(&product_number); sleep(1); //sleep(rand()%5); } }void *consumer(void *arg){ int c = 0, i; while(1) { sem_wait(&product_number); for(i=0; i < NUM; i++) { printf("%d ", queue[i]); } putchar('\n'); printf("consume %d\n", queue[c]); queue[c] = 0; sem_post(&blank_number); c = (c+1)%NUM; sleep(3); //sleep(rand()%5); }}int main(){ pthread_t pid, cid; // 可用资源数为 NUM = 5 sem_init(&blank_number, 0, NUM); sem_init(&product_number, 0, 0); pthread_create(&pid, NULL, producer, NULL); pthread_create(&cid, NULL, consumer, NULL); pthread_join(pid, NULL); pthread_join(cid, NULL); sem_destroy(&blank_number); sem_destroy(&product_number); return 0;}
运行结果:
2、用Condition Variable实现Semaphore ,即用pthread_cond_wait()等方式实现sem_wait()等方式。
#include <stdlib.h>#include <pthread.h>#include <stdio.h>#include <unistd.h>#define NUM 5int queue[NUM];int current_num; pthread_cond_t has_product = PTHREAD_COND_INITIALIZER;pthread_cond_t blank_product = PTHREAD_COND_INITIALIZER;pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;void *producer(void *arg){ int p = 0, i; while(1){ pthread_mutex_lock(&lock); while(current_num >= NUM) { pthread_cond_wait(&blank_product, &lock); } queue[p] = rand() % 1000 + 1; printf("produce %d\n", queue[p]); p = (p + 1) % NUM; current_num ++; pthread_mutex_unlock(&lock); printf("after produce --- current_num: %d\n", current_num); for(i=0; i < NUM; i++) { printf("%d ", queue[i]); } putchar('\n'); sleep(1); //sleep(rand()%5); pthread_cond_signal(&has_product); } }void *consumer(void *arg){ int c = 0, i; while(1) { pthread_mutex_lock(&lock); pthread_cond_wait(&has_product, &lock); printf("consume %d\n", queue[c]); queue[c] = 0; c = (c+1)%NUM; current_num--; printf("after consume --- current_num: %d\n", current_num); for(i=0; i < NUM; i++) { printf("%d ", queue[i]); } putchar('\n'); pthread_mutex_unlock(&lock); sleep(3); //sleep(rand()%5); pthread_cond_signal(&blank_product); }}int main(){ pthread_t pid, cid; pthread_create(&pid, NULL, producer, NULL); pthread_create(&cid, NULL, consumer, NULL); pthread_join(pid, NULL); pthread_join(cid, NULL); printf("----\n"); return 0;}
运行结果:
0 0
- [Linux]多线程同步之sem_wait()学习笔记
- [Linux]多线程同步之pthread_cond_wait()学习笔记
- linux多线程学习笔记三---线程同步之信号量
- java 多线程学习笔记之 线程同步
- linux多线程之同步
- linux多线程学习笔记四---线程同步之互斥锁、读写锁和条件变量
- Linux程序设计学习笔记----多线程编程线程同步机制之互斥量(锁)与读写锁
- Linux程序设计学习笔记----多线程编程之线程同步条件变量
- linux SEM_WAIT
- 学习笔记之Linux内核同步方法
- 多线程开发学习笔记之线程同步——互斥量
- 多线程开发学习笔记之线程同步——信号量
- 多线程开发学习笔记之线程同步——事件
- C#多线程学习笔记(二)之线程同步
- 线程同步之信号量(sem_init,sem_post,sem_wait)
- 线程同步之信号量(sem_init,sem_post,sem_wait)
- linux多线程学习笔记
- Linux多线程学习笔记
- 【Jsp+Servlet】JSP的内置对象
- Android官方MVP架构示例项目解读
- 高频系数融合规则
- 再次学习java语言中值传递和引用传递
- actionscript 3.0方向键控制移动
- [Linux]多线程同步之sem_wait()学习笔记
- 欢迎使用CSDN-markdown编辑器
- 如何使用信号量处理问题
- 软考的大潮究竟给我带来了什么
- 【软考总结】
- servlet中的<load-on-startup>配置
- 软考二进宫,再进帝都
- uoj49四驱兄弟(最小生成树)
- CCF俄罗斯方块C++版