为什么我在使用pthread_cond_signal和pthread_cond_wait时遇到死锁?

已解决 我在同步方面遇到一些问题。

首先,该文件的目的是创建10个线程,并在偶数编号之前打印奇数编号。

据我所知,我的pthread_cond_wait正在创建死锁。通过在threadFunction中包含以下信号,我能够解决此死锁。 如果我不包括此内容,则在打印所有奇数线程ID后,我将无法运行该程序。

        perror("Unable to pthread_cond_broadcast");
        exit(EXIT_FAILURE);
      }

更新的部分-解决方案

void *threadFunction(void *threadId) {
    if ((long) threadId % 2 == 0 && remainingOddThreads > 0) {
        pthread_mutex_lock(&lock);
        if (pthread_cond_wait(&evenCanGo,&lock)) {
            perror("Error setting a pthread_cond_wait:");
            exit(EXIT_FAILURE);
        }
        printf("Hello from thread[%ld]!\n",(long) threadId);
        pthread_mutex_unlock(&lock);
    } else {
        if (pthread_mutex_lock(&lock)) {
            perror("Unable to lock mutex:");
            exit(EXIT_FAILURE);
        }
        printf("Hello from thread[%ld]!\n",(long) threadId);
        remainingOddThreads--;
        if (pthread_mutex_unlock(&lock)) {
            perror("Unable to unlock mutex:");
            exit(EXIT_FAILURE);
        }
    }
    if (remainingOddThreads == 0) {
        pthread_mutex_lock(&lock);
        if (pthread_cond_signal(&evenCanGo)) {
            perror("Unable to pthread_cond_broadcast");
            exit(EXIT_FAILURE);
        }
        pthread_mutex_unlock(&lock);
    }
    return NULL;
}

这是OLD代码文件

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

#define NUMber_OF_ODD_NUMberS_FROM_ONE_TO_TEN 5;
#define NUMber_OF_THREADS 10
int remainingOddThreads = NUMber_OF_ODD_NUMberS_FROM_ONE_TO_TEN
pthread_t threads[NUMber_OF_THREADS];
pthread_mutex_t lock = pthREAD_MUTEX_INITIALIZER;
pthread_cond_t evenCanGo = pthREAD_COND_INITIALIZER;

void *threadFunction(void *threadId) {
    if ((long) threadId % 2 == 0 && remainingOddThreads > 0) {
        if (pthread_cond_wait(&evenCanGo,(long) threadId);
    } else {
        if (pthread_mutex_lock(&lock)) {
            perror("Unable to lock mutex:");
            exit(EXIT_FAILURE);
        }
        printf("Hello from thread[%ld]!\n",(long) threadId);
        remainingOddThreads--;
        if (pthread_mutex_unlock(&lock)) {
            perror("Unable to unlock mutex:");
            exit(EXIT_FAILURE);
        }
    }
    if (pthread_cond_signal(&evenCanGo)) {
        perror("Unable to pthread_cond_broadcast");
        exit(EXIT_FAILURE);
    }
    return NULL;
}


void createThreads() {
    for (long i = 1; i <= NUMber_OF_THREADS; i++) {
        int status = pthread_create(&threads[i - 1],NULL,threadFunction,(void *) i);
        if (status) {
            perror("Failed to create thread");
            exit(EXIT_FAILURE);
        }
    }
}

void activateThreads() {
    for (long i = 0; i < NUMber_OF_THREADS; i++) {
        int status = pthread_join(threads[i],NULL);
        if (status > 0) {
            perror("Unable to join thread");
            exit(EXIT_FAILURE);
        }
    }
}


int main() {
    createThreads();
    activateThreads();

    if (pthread_cond_destroy(&evenCanGo) != 0) {
        perror("Unable to destroy pthread_cond_t object:");
        exit(EXIT_FAILURE);
    }
    pthread_mutex_unlock(&lock);
    if (pthread_mutex_destroy(&lock) != 0) {
        perror("Unable to destroy pthread_mutex_t object:");
        printf("%d",errno);
        exit(EXIT_FAILURE);
    }
    return 0;
}
sugartian 回答:为什么我在使用pthread_cond_signal和pthread_cond_wait时遇到死锁?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/1470979.html

大家都在问