首页 > 代码库 > thread_22
thread_22
#include <stdlib.h>#include <pthread.h>#include <stdio.h>#include <sched.h>pthread_cond_t cond;pthread_mutex_t mp; void *consumer1(void *p) { pthread_mutex_lock(&mp); printf("wait>>>(1)\n"); pthread_cond_wait(&cond, &mp); pthread_mutex_unlock(&mp); printf("wake <<<(1)\n"); return NULL; } void *consumer2(void *p) { pthread_mutex_lock(&mp); printf("wait>>>(2)\n"); pthread_cond_wait(&cond, &mp); pthread_mutex_unlock(&mp); printf("wake <<<(2)\n"); return NULL;}void *consumer3(void *p) { pthread_mutex_lock(&mp); printf("wait>>>(3)\n"); pthread_cond_wait(&cond, &mp); pthread_mutex_unlock(&mp); printf("wake <<<(3)\n"); return NULL; } int main(int argc, char *argv[]) { pthread_t t1, t2, t3; int ret; struct sched_param sched; sched.__sched_priority = 10; pthread_cond_init(&cond, NULL); pthread_mutex_init(&mp, NULL); ret = pthread_create(&t1, NULL, consumer1, NULL); ret = pthread_create(&t2, NULL, consumer2, NULL); ret = pthread_create(&t3, NULL, consumer3, NULL); sleep(1); pthread_setschedparam(t2, SCHED_FIFO, &sched); sleep(1); pthread_cond_broadcast(&cond); sleep(6); printf("main returned\n"); return 0;}
thread_22
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。