首页 文章

pthread互斥锁在等待时无法解锁

提问于
浏览
2

我正在使用c posix线程,我遇到了一个我不太了解的问题,想要一些新鲜的眼睛来看看问题是什么 .

总之,我正在创建3个线程来检查某些程序状态,发出信号表明它们已准备就绪,然后等待更多信号 . 同时,创建这些线程的线程正在等待(pthread_cond_wait_)每个线程发出信号表明它已经被设置 . 但是,一旦发出等待信号,似乎永远不会解锁互斥锁,导致程序完全停止 .

这是主线程的代码:

pthread_mutex_lock(&scenario->mutex);
    if (scenario->flags.sinkThreadSetup == 0) {
        pthread_create(&sinkT, NULL, sinkProblemHandler, (void *)scenario);
        pthread_cond_wait(&scenario->sinkSetupCondition, &scenario->mutex);
    }
    if (scenario->flags.freeThreadSetup == 0) {
        pthread_create(&freeT, NULL, freeWheelProblemHandler, (void *)scenario);
        pthread_cond_wait(&scenario->freeSetupCondition, &scenario->mutex);
    }
    if (scenario->flags.blockThreadSetup == 0) {
        pthread_create(&blockT, NULL, blockProblemHandler, (void *)scenario);
        pthread_cond_wait(&scenario->blockSetupCondition, &scenario->mutex);
    }
    scenario->state = VECTORING;
    pthread_mutex_unlock(&scenario->mutex);

    // Start wheel threads
    pthread_t wheelThreads[NUM_WHEELS];
    scenario_wheel_t threadDataArr[NUM_WHEELS];
    for (int i =0; i < NUM_WHEELS; i++) {
        threadDataArr[i].scenario = scenario;
        threadDataArr[i].wheel = &scenario->wheels[i];
        pthread_create(&wheelThreads[i], NULL, wheel_start, (void *)&threadDataArr[i]);
    }
    pthread_mutex_lock(&scenario->mutex);
    pthread_cond_wait(&scenario->scenarioComplete_condition, &scenario->mutex);
    pthread_mutex_unlock(&scenario->mutex);

以下是问题处理程序线程的代码:

void *sinkProblemHandler(void *args) {
    scenario_t *scenario = (scenario_t *) args;
    while(scenario->flags.terminate != 1) {
        pthread_mutex_lock(&scenario->mutex);
        if (scenario->state == SETUP && scenario->flags.sinkThreadSetup == 0) {
            scenario->flags.sinkThreadSetup = 1;
            pthread_cond_signal(&scenario->sinkSetupCondition);
        }
        if (scenario->state != SINKING) {
            pthread_cond_wait(&scenario->conditions.sinking_condition, &scenario->mutex);
            if (scenario->flags.terminate == 1) {
                pthread_mutex_unlock(&scenario->mutex);
                pthread_exit(NULL);
            }
            printf("SINKHandler: I'M HELPING!");
        }
        pthread_mutex_unlock(&scenario->mutex);
    }
    pthread_exit(NULL);
}

void *blockProblemHandler(void *args) {
    scenario_t *scenario = (scenario_t *) args;
    while(scenario->flags.terminate != 1) {
        pthread_mutex_lock(&scenario->mutex);
        if (scenario->state == SETUP && scenario->flags.blockThreadSetup == 0) {
            scenario->flags.blockThreadSetup = 1;
            pthread_cond_signal(&scenario->blockSetupCondition);
        }
        if (scenario->state != BLOCKED) {
            pthread_cond_wait(&scenario->conditions.blocked_condition, &scenario->mutex);
            if (scenario->flags.terminate == 1) {
                pthread_mutex_unlock(&scenario->mutex);
                pthread_exit(NULL);
            }
            printf("BlockHandler: I'M HELPING!");
        }
        pthread_mutex_unlock(&scenario->mutex);
    }
    pthread_exit(NULL);
}

void *freeWheelProblemHandler(void * args) {
    scenario_t *scenario = (scenario_t *) args;
    while(scenario->flags.terminate != 1) {
        pthread_mutex_lock(&scenario->mutex);
        if (scenario->state == SETUP && scenario->flags.freeThreadSetup == 0) {
            scenario->flags.freeThreadSetup = 1;
            pthread_cond_signal(&scenario->freeSetupCondition);
        }
        if (scenario->state != BLOCKED) {
            pthread_cond_wait(&scenario->conditions.freeWheeling_condition, &scenario->mutex);
            if (scenario->flags.terminate == 1) {
                pthread_mutex_unlock(&scenario->mutex);
                pthread_exit(NULL);
            }
            printf("FreeHandler: I'M HELPING!");
        }
        pthread_mutex_unlock(&scenario->mutex);
    }
    pthread_exit(NULL);
}

我们可以在这里看到问题处理程序线程发出信号它们各自的设置条件,然后等待不同的条件(pthread_cond_wait),这应解锁互斥锁 .

我最初的想法是问题处理程序线程在主线程开始等待之前发出信号,从而产生死锁 . 但是,在附加到程序之后,情况并非如此,因为程序会:
1.启动问题线程
2.等待问题设置条件(解锁互斥锁)
3.问题处理程序线程锁互斥锁 .
3.问题处理程序线程然后信号设置完成
4.问题处理程序线程然后等待(据说解锁互斥锁)
5.程序完全停止 . (就像互斥锁没有解锁一样) .

任何帮助将不胜感激 . 谢谢

1 回答

  • 1

    你的工作线程大致都具有

    while(var) {
      pthread_mutex_lock(&scenario->mutex);
      // ...
      pthread_mutex_unlock(&scenario->mutex);
    }
    

    这是互斥锁在解锁后立即被锁定 . 主线程有一点机会继续执行,因为它无法重新获取互斥锁 .

    这是 man pthread_cond_signal

    未阻塞的线程应根据调度策略(如果适用)竞争互斥锁,并且好像每个都调用了pthread_mutex_lock() .

    所以,至少可以在重新获取工作线程中的互斥锁之前尝试 pthread_yield() ;甚至可以更好地减少互斥锁的使用和/或引入不同的互斥锁,让不同的部分并行运行 .

相关问题