📅  最后修改于: 2023-12-03 15:12:20.046000             🧑  作者: Mango
在一家理发店中,有三个理发师和若干个等待理发的顾客。理发师需要对顾客进行理发,但每个理发师只能同时为一个顾客服务。如果有多个顾客等待理发,则只能有一个顾客能够进入理发店等待理发。如果所有理发师都没有顾客可为其服务,则理发师需要睡眠并等待顾客的到来。
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void *barber(void *arg) {
char *name = (char *)arg;
while (1) {
pthread_mutex_lock(&mutex);
if (waiting == 0) {
printf("%s: Zzzzzz...\n", name);
pthread_cond_wait(&cond, &mutex);
printf("%s: Wake up!\n", name);
}
waiting--;
seats++;
printf("%s: Cutting hair now. (%d/%d)\n", name, waiting, seats);
pthread_mutex_unlock(&mutex);
sleep(1);
}
pthread_exit(NULL);
}
void *customer(void *arg) {
pthread_t tid = pthread_self();
pthread_mutex_lock(&mutex);
if (seats == 0) {
printf("Thread %lu: There is no seat. (waiting: %d)\n", tid, waiting);
pthread_mutex_unlock(&mutex);
return NULL;
}
waiting++;
seats--;
printf("Thread %lu: Sit down. (waiting: %d)\n", tid, waiting);
pthread_mutex_unlock(&mutex);
pthread_cond_signal(&cond);
pthread_mutex_lock(&mutex);
while (waiting) {
pthread_cond_wait(&cond, &mutex);
}
printf("Thread %lu: Leave. (waiting: %d)\n", tid, waiting);
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
int main() {
pthread_t tid1, tid2, tid3;
pthread_t customers[15];
int i = 0;
int ret;
ret = pthread_create(&tid1, NULL, barber, "Barber 1");
if (ret != 0) {
printf("Create barber 1 failed.\n");
return -1;
}
ret = pthread_create(&tid2, NULL, barber, "Barber 2");
if (ret != 0) {
printf("Create barber 2 failed.\n");
return -1;
}
ret = pthread_create(&tid3, NULL, barber, "Barber 3");
if (ret != 0) {
printf("Create barber 3 failed.\n");
return -1;
}
while (1) {
ret = pthread_create(&customers[i], NULL, customer, NULL);
if (ret != 0) {
printf("Create customer %d failed.\n", i);
return -1;
}
sleep(1);
i++;
if (i == 15) {
break;
}
}
ret = pthread_join(tid1, NULL);
if (ret != 0) {
printf("Join barber 1 failed.\n");
return -1;
}
ret = pthread_join(tid2, NULL);
if (ret != 0) {
printf("Join barber 2 failed.\n");
return -1;
}
ret = pthread_join(tid3, NULL);
if (ret != 0) {
printf("Join barber 3 failed.\n");
return -1;
}
return 0;
}
本文介绍了进程同步中的睡眠理发师问题,并提供了基本思路和代码实现。在实现过程中,我们需要考虑多线程竞争资源的问题,通过设置锁和条件变量,保证线程安全。同时也要注意防止死锁的发生。在编写多线程程序时,我们需要综合考虑线程安全和程序的效率,选择合适的同步机制来保证线程安全的同时尽可能减少线程的等待时间。