【问题标题】:Program with multiple threads always giving same output具有多个线程的程序总是给出相同的输出
【发布时间】:2020-04-29 17:02:37
【问题描述】:

我正在学习多线程并尝试创建一个可以交替打印两个字符串的程序。 我写了以下代码:

#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_mutex_t lock;
void print(char a[50]){
    pthread_mutex_lock(&lock);
    printf("%s", a);    //output console is the shared resource
    sleep(2);
    pthread_mutex_unlock(&lock);
}
void* hello(void* status){
    while(*((char*)status) != '\n'){ 
        print("Hello World\n"); 
    }
}
void* bye(void* status){
    while(*((char*)status) != '\n'){
        print("Goodbye World\n");
    }
}
int main(){
    pthread_t id1, id2;
    char status = '\0';
    int state;
    if (pthread_mutex_init(&lock, NULL) != 0) { 
        printf("\n mutex init has failed\n"); 
        exit(1); 
    }
    printf("Starting Threads (Press Enter to terminate)\n");
    state = pthread_create(&id1, NULL, hello, &status);
    if(state != 0){
        printf("Could not create thread, exiting.\n");
        exit(1);
    }
    state = pthread_create(&id2, NULL, bye, &status);
    if(state != 0){
        printf("Could not create thread, exiting.\n");
        exit(1);
    }
    scanf("%c", &status);
    printf("Out of The Threads\n");
    pthread_mutex_destroy(&lock); 
    return 0;
}

据我了解,互斥锁应该为 hello 函数锁定 print 函数一次,然后为 bye 函数锁定一次。但我只得到这个输出:

Starting Threads (Press Enter to terminate)
Hello World
Hello World
Hello World
Hello World
Hello World

为什么只有 hello 函数被分配了 print 函数?我如何让它同时打印?

【问题讨论】:

标签: c multithreading mutex thread-synchronization


【解决方案1】:

因为,您在每个函数 hellobye 中使用不定式 while。当第一个线程启动时,它永远不会退出while 循环,除非您按下enter

但是当你输入enter时,所有函数中的循环条件都会是false,所以两个线程永远不会再接触print函数。

OT,你忘记加入话题了。

尝试删除每个函数中的while循环并在main函数中使用它,看看会发生什么。例如下面的程序(它可能没有优化,但我只是想说明为什么你的程序总是打印Hello World

#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_mutex_t lock;
void print(char a[50]){
    pthread_mutex_lock(&lock);
    printf("%s", a);    //output console is the shared resource
    sleep(2);
    pthread_mutex_unlock(&lock);
}
void* hello(void* status){
   // while(*((char*)status) != '\n'){ 
        print("Hello World\n"); 
    //}
}
void* bye(void* status){
    //while(*((char*)status) != '\n'){
        print("Goodbye World\n");
//    }
}
int main(){
    pthread_t id1, id2;
    char status = '\0';
    int state;
    if (pthread_mutex_init(&lock, NULL) != 0) { 
        printf("\n mutex init has failed\n"); 
        exit(1); 
    }
    printf("Starting Threads (Press Enter to terminate)\n");
    while(status != '\n') {
        state = pthread_create(&id1, NULL, hello, &status);
        if(state != 0){
           printf("Could not create thread, exiting.\n");
           exit(1);
        }
        state = pthread_create(&id2, NULL, bye, &status);
        if(state != 0){
           printf("Could not create thread, exiting.\n");
           exit(1);
        }
        pthread_join(id1, NULL);
        pthread_join(id1, NULL);
        scanf("%c", &status);
    }

    printf("Out of The Threads\n");
    pthread_mutex_destroy(&lock); 
    return 0;
}

【讨论】:

    猜你喜欢
    • 2015-05-15
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多