gpt4 book ai didi

c - pthread 互斥体在等待时未解锁

转载 作者:行者123 更新时间:2023-12-04 23:52:55 25 4
gpt4 key购买 nike

我正在使用 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. 程序完全停止。 (就像互斥量没有被解锁一样)。

如有任何帮助,我们将不胜感激。谢谢

最佳答案

你的工作线程大致都是这样的形式

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

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

这来自man pthread_cond_signal:

The thread(s) that are unblocked shall contend for the mutex according to the scheduling policy (if applicable), and as if each had called pthread_mutex_lock().

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

关于c - pthread 互斥体在等待时未解锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42889436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com