【问题标题】:Multithreading printf() with semaphores in cc语言中带有信号量的多线程printf()
【发布时间】:2018-11-23 03:39:03
【问题描述】:

我想要实现的是三个独立的打印一个接一个地执行,打印消息“多么美好的世界!”。相反,我得到的是“多么美妙的美妙世界世界世界”。我认为 pthread_join() 有问题。当用户输入 ctrl+c 时程序将结束。

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <semaphore.h>

sem_t Asem;
sem_t Bsem;
sem_t Csem;

void *print_WhatA(){
    while(1){
        sem_wait(&Asem);
        printf("What A ");
        sem_post(&Bsem);
    }
} 

void *print_Wonderful(){
    while(1){
        sem_wait(&Bsem);
        printf("Wonderful ");
        sem_post(&Csem);
    }
}

void *print_World(){
    while(1){
        sem_wait(&Csem);
        printf("World!\n");
        sem_post(&Asem);
    }
}

void main(){
    sem_init(&Asem,0,0);    //initialization of the first semaphore
    sem_init(&Bsem,0,1);    //initialization of the second semaphore
    sem_init(&Csem,0,2);    //initialization of the third semaphore
    pthread_t A_thread;    
    pthread_t B_thread;
    pthread_t C_thread;
    pthread_create(&A_thread,NULL, print_WhatA,NULL);       //thread creation for print_WhatA function 
    pthread_create(&B_thread,NULL, print_Wonderfull,NULL);  //thread creation for print_Wonderful function
    pthread_create(&C_thread,NULL, print_World,NULL);       //thread creation for print_World function
    pthread_join(A_thread,NULL);
    pthread_join(B_thread,NULL);
    pthread_join(C_thread,NULL);
}

【问题讨论】:

  • 如果你只想让三件事一个接一个地发生,不要创建任何线程。线程用于在另一件事之后、之前或同时发生的事情。如果您已经有线程并且希望一个线程中的某些事情在另一个线程中的其他事情之后发生,那么信号量是该工作的错误工具。你需要条件变量。
  • @n.m. “信号量不适合这项工作”——不,它们是一种正确的工具,正如 PSkocik 在答案中所展示的那样。
  • @MartinJames 我的错,我读信号量并认为互斥量。

标签: c linux multithreading semaphore


【解决方案1】:

您应该将信号量 B 和 C 初始化为 0,将信号量 A 初始化为 1。 这有效:

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <semaphore.h>

sem_t Asem;
sem_t Bsem;
sem_t Csem;

void *print_WhatA(){
    while(1){
        sem_wait(&Asem);
        printf("What A ");
        sem_post(&Bsem);
    }
} 

void *print_Wonderful(){
    while(1){
        sem_wait(&Bsem);
        printf("Wonderful ");
        sem_post(&Csem);
    }
}

void *print_World(){
    while(1){
        sem_wait(&Csem);
        printf("World!\n");
        sem_post(&Asem);
    }
}

void main(){
    sem_init(&Asem,0,1);    //initialization of the first semaphore
    sem_init(&Bsem,0,0);    //initialization of the second semaphore
    sem_init(&Csem,0,0);    //initialization of the third semaphore
    pthread_t A_thread;    
    pthread_t B_thread;
    pthread_t C_thread;
    pthread_create(&A_thread,NULL, print_WhatA,NULL);       //thread creation for print_WhatA function 
    pthread_create(&B_thread,NULL, print_Wonderful,NULL);  //thread creation for print_Wonderful function
    pthread_create(&C_thread,NULL, print_World,NULL);       //thread creation for print_World function
    pthread_join(A_thread,NULL);
    pthread_join(B_thread,NULL);
    pthread_join(C_thread,NULL);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    • 2010-10-15
    • 1970-01-01
    • 2013-12-12
    • 1970-01-01
    • 2013-06-27
    • 1970-01-01
    相关资源
    最近更新 更多