网站文件上传好下一步怎么做,征婚网站上拉业务做恒指期货,怎么查询公司名字是否可以注册,网页设计师考试内容linux知识#xff08;二#xff09;互斥量、信号量和生产者消费者模型一、互斥量产生原因二、信号量生产者消费者模型一、互斥量
产生原因
使用多线程常常会碰到数据混乱的问题#xff0c;那么使用互斥量#xff0c;相当于“加锁”的操作#xff0c;将有助于解决数据混乱…
linux知识二互斥量、信号量和生产者消费者模型一、互斥量产生原因二、信号量生产者消费者模型一、互斥量
产生原因
使用多线程常常会碰到数据混乱的问题那么使用互斥量相当于“加锁”的操作将有助于解决数据混乱的问题 每个线程在对资源操作前都尝试先加锁成功加锁才能操作操作结束解锁 资源还是共享的线程间也还是竞争的 但通过“锁”就将资源的访问变成互斥操作而后与时间有关的错误也不会再产生了。
#include stdio.h
#include string.h
#include pthread.h
#include stdlib.h
#include unistd.hpthread_mutex_t mutex; //定义锁
void* tfn(void* arg)
{srand(time(NULL));while (1) {pthread_mutex_lock(mutex);printf(hello );sleep(rand() % 3); /*模拟长时间操作共享资源导致cpu易主产生与时间有关的错误*/printf(world\n);pthread_mutex_lock(mutex);pthread_mutex_unlock(mutex);sleep(rand() % 3);}return NULL;
}int main(void)
{int flg 5;pthread_t tid;srand(time(NULL));pthread_mutex_init(mutex,NULL); //mutex1,初始化锁pthread_create(tid,NULL,tfn,NULL);while (1) {pthread_mutex_lock(mutex);printf(HELLO );sleep(rand() % 3);printf(WORLD\n);pthread_mutex_unlock(mutex);sleep(rand() % 3);}pthread_cancel(tid);pthread_join(tid, NULL);pthread_mutex_destroy(mutex);return 0;
}二、信号量
互斥量的操作是1对1的一个线程加锁与解锁完成后下一个线程通过竞争在拿到锁进行操作对于多线程并发来说无形间效率就变低了。多个线程间对某一对象的部分数据进行共享使用互斥锁是没有办法实现的只能将整个数据对象锁住。线程从并行执行变成了串行执行。与直接使用单进程无异。信号量能有效的解决这一问题。
生产者消费者模型
说到信号量就必须提到生产者消费者模型 我们来假设一下这样一个情景图中有生产者消费者以及有5个盘子。生产者只有生产了消费者才能拿到东西。我们定义5个线程来当作生产者持续生产它们需要拿到盘子来生产它们不在像用拿“锁”的方式一个一个来生产这样无形之中就提高了效率提高了并发型。消费等生产者生产完通过竞争来抢这些消费品。
#include cstdio
#include pthread.h
#include iostream
#include stdio.h
#include stdlib.h
#include unistd.h
#include semaphore.h
#include string.husing namespace std;
int g_count0;
pthread_mutex_t prod_mutex;
pthread_mutex_t cur_mutex;
sem_t g_sem_prod;//生产者信号量
sem_t g_sem_cur; //消费者的信号量void * add(void *arg)
{//生产者cout thread .. wait .. id pthread_self() endl;//开启保护线程sem_wait(g_sem_prod);pthread_mutex_lock(prod_mutex);g_count;printf(make cake ... g_count %d\n,g_count);//解除保护pthread_mutex_unlock(prod_mutex);sem_post(g_sem_cur);sleep(rand() % 3);}void* sub(void* arg)
{//消费者cout wait cake thread .. wait .. id pthread_self() endl;sem_wait(g_sem_cur);//开启保护线程pthread_mutex_lock(cur_mutex);g_count--;printf(eat.......g_count %d\n, g_count);//解除保护pthread_mutex_unlock(cur_mutex);sem_post(g_sem_prod);sleep(rand() % 3);}int main()
{int i 0;pthread_t tid;int ret;pthread_mutex_init(prod_mutex,NULL);pthread_mutex_init(cur_mutex, NULL);sem_init(g_sem_prod,0,5);sem_init(g_sem_cur,0, 0);//线程 加一操作for (i 0; i 5; i){ret pthread_create(tid, NULL, add, NULL);if (ret ! 0){cout strerror(ret) endl;return 0;}}//消费者for (i 0; i 5; i){retpthread_create(tid, NULL, sub, NULL);if (ret ! 0){cout strerror(ret) endl;return 0;}}while (1){}return 0;
}