#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; }
时间: 2024-10-20 10:16:47