当前位置: 首页> 财经> 金融 > 太平保险网站_书签怎么制作教程_营销网站建设推广_福州seo推广

太平保险网站_书签怎么制作教程_营销网站建设推广_福州seo推广

时间:2025/8/27 14:23:31来源:https://blog.csdn.net/qq_36369228/article/details/142560156 浏览次数:0次
太平保险网站_书签怎么制作教程_营销网站建设推广_福州seo推广

条件变量不是锁,但通常结合锁使用,条件变量用于检查某个条件是否满足。

条件变量基本函数
int pthread_cond_init(pthread_cond_t *restrict cond, pthread_condattr_t *restrict attr);// 动态初始化条件变量,参数cond:条件变量的指针,参数attr:条件变量的初始属性。成功返回0,失败错误号。
int pthread_cond_destory(pthread_cond_t *cond);// 销毁条件变量,参数cond:条件变量的指针。成功返回0,失败错误号。
int pthread_cond_signal(pthread_cond_t *cond);// 条件满足后,可以使用该函数通知至少一个线程条件已满足,线程收到通知后会被唤醒
int pthread_cond_broadcast(pthread_cond_t *cond);// 条件满足后广播通知所有线程。pthread_cond_t cond = PTHREAD_COND_INITIALIZER; // 静态初始化
pthread_cond_wait函数
int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);// 阻塞式等待条件变量cond满足,满足后释放互斥锁mutex,成功返回0,失败错误号。

pthread_cond_wait函数中的阻塞式检查条件满足和释放互斥锁为一个原子操作。释放互斥锁后,解除阻塞,重新加锁。

pthread_cond_timewait函数
int pthread_cond_timewait(phthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, const struct timespec *restrict abstime); // 成功返回0,失败错误号

作用:在某个时间内阻塞式等待条件成立,若超时则条件不满足也返回并唤醒线程。

条件变量在生产者消费者模型中的应用:

在生产者消费者问题中,若缓冲区为空,则消费者无法消费,于是消费者线程应使用pthread_cond_wait阻塞式等待条件:缓冲区不为空满足。生产者生产一个产品后,缓冲区不为空条件满足,于是使用pthread_cond_signal唤醒之前由于缓冲区为空而被阻塞的消费者线程。

流程为:

消费者:

  1. 创建一个条件变量:pthread_cond_t cond 并初始化条件变量:pthread_cond_init -该操作是全局的
  2. 创建一个互斥锁:pthread_mutex_t mutex 并初始化互斥锁:pthread_mutex_init -该操作是全局的
  3. 加锁:pthread_mutex_lock;
  4. 消费者线程使用pthread_cond_wait阻塞式等待缓冲区不为空的条件发生
  5. 若消费者线程要访问的缓冲区为空,生产者线程生产一个产品放入缓冲区,使用pthread_cond_signal唤醒刚刚进入阻塞的消费者线程。
  6. 若消费者线程要访问的缓冲区不为空,消费者线程直接被唤醒;
  7. 消费者线程的wait函数收到唤醒消息后,wait函数加锁,消费者线程访问共享数据(缓冲区)。
  8. 解锁pthread_mutex_unlock,释放条件变量pthread_cond_destory,释放锁pthread_mutex_destory。

生产者:

  1. 生产数据;
  2. 加锁pthread_mutex_lock;
  3. 将数据放入缓冲区;
  4. 解锁pthread_mutex_unlock,并通知阻塞中的消费者线程pthread_cond_signal/pthread_cond_boardcast。
  5. 循环生产后续数据.

程序示例1:
代码:
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<stdio.h>struct msg{struct msg *next;int num;
};struct msg *head;pthread_cond_t has_product = PTHREAD_COND_INITIALIZER;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;void* consumer(void* p){struct msg *mp;for(;;){pthread_mutex_lock(&lock);while(head == NULL){pthread_cond_wait(&has_product, &lock);}// 消费一个产品mp = head;head = mp->next;pthread_mutex_unlock(&lock);printf("-Consume %lu ---%d\n", pthread_self(), mp->num);free(mp);sleep(rand() % 5);}}void* producer(void* p){struct msg *mp;for(;;){mp = malloc(sizeof(struct msg));mp->num = rand() % 1000 + 1;printf("-Produce ----------------%d\n", mp->num);pthread_mutex_lock(&lock);mp->next = head;head = mp;pthread_mutex_unlock(&lock);pthread_cond_signal(&has_product);sleep(rand() % 5);}}int main(int argc, char *argv[]){pthread_t pid, cid;srand(time(NULL));for(int i = 0; i < 3; i++){pthread_create(&pid, NULL, producer, NULL);}for(int i = 0; i < 3; i++){pthread_create(&cid, NULL, consumer, NULL);}pthread_join(pid, NULL);pthread_join(cid, NULL);return 0;
}
执行结果:

程序示例2:
代码:
#include<pthread.h>const int BUFFER_MAXNUM = 3;pthread_cond_t empty_cond = PTHREAD_COND_INITIALIZER, full_cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;int empty = BUFFER_MAXNUM, full = 0;int cnt = 0;void* productor(void* arg){while(1){pthread_mutex_lock(&mutex);--empty;if(empty < 0){printf("生产者%d 等待缓冲区不为满\n", (int)arg);pthread_cond_wait(&full_cond, &mutex);printf("生产者%d 已等到缓冲区不为满\n", (int)arg);}// 生产printf("生产者%d, product %dth\n", (int)arg, ++cnt);// 将数据放入缓冲区if(++full <= 0){pthread_cond_signal(&empty_cond);}pthread_mutex_unlock(&mutex);sleep(1);}
}void* consumer(void* arg){while(1){pthread_mutex_lock(&mutex);if(--full < 0){printf("消费者%d 等待缓冲区不为空\n", (int)arg);pthread_cond_wait(&empty_cond, &mutex);printf("消费者%d 已等到缓冲区不为空\n", (int)arg);}// 消费一个产品printf("消费者%d, cast %dth\n", (int)arg, cnt--);if(++empty <= 0){pthread_cond_signal(&full_cond);}pthread_mutex_unlock(&mutex);sleep(1);}
}int main(){pthread_t pdr, csr;for(int i = 0; i < 3; i++){pthread_create(&pdr, NULL, productor, (void*)i);}for(int i = 0; i < 5; i++){pthread_create(&csr, NULL, consumer, (void*)i);}pthread_exit(NULL);
}
执行结果:

关键字:太平保险网站_书签怎么制作教程_营销网站建设推广_福州seo推广

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: