gpt4 book ai didi

c - 在具有4个线程的程序上调用pthread_cond_signal时,同一线程将获得互斥体

转载 作者:行者123 更新时间:2023-12-03 12:54:09 24 4
gpt4 key购买 nike

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

pthread_t node[4];
pthread_mutex_t token;
pthread_cond_t cond;
int id=0;

void *func(int n)
{
int count = 0;
while (count < 10){
pthread_mutex_lock(&token);
while (id != n){
printf("Whoops not my turn, id=%d\n",n);
pthread_cond_wait(&cond, &token);}
//if (id == n){
count += 1;
printf ("My turn! id= %d\n",n);
printf("count %d\n", count);
if (id == 3){
id = 0;}
else{
id += 1;}
//}else{
// printf("Not my turn! id=%d\n",n);}
// pthread_mutex_unlock(&token);
// sleep(2);}
pthread_mutex_unlock(&token);
pthread_cond_signal(&cond);}
printf ("ID=%d has finished\n",n);

return(NULL);
}

int main()
{
int i;
pthread_mutex_init(&token,NULL);
pthread_cond_init(&cond,NULL);
for(i=0;i<4;i++)
pthread_create(&node[i],NULL,(void *)func,(void *)i);

for(i=0;i<4;i++)
pthread_join(node[i],NULL);

pthread_mutex_destroy(&token);

return 0;
}

这是我的代码,它是一个使用线程的C程序。我认为这里不需要知道它的目的,但是我将举一个例子说明我遇到的问题。

说id为1(在func中由n定义)的线程获取互斥锁,并返回“我的转弯!id = 1”。当调用mutex_unlock和cond_signal时,下一个获取互斥锁的线程实际上将再次是ID为1的线程,并且它将显示“Whoops not my turn,id = 1”。只有这样,ID为2的线程才能获得互斥锁,并打印“My turn!id = 2”,但是当ID = 2的线程将获得互斥锁。这是我的程序输出:
Whoops not my turn, id=1
Whoops not my turn, id=2
Whoops not my turn, id=3
My turn! id= 0
count 1
Whoops not my turn, id=0
My turn! id= 1
count 1
Whoops not my turn, id=1
My turn! id= 2
count 1
Whoops not my turn, id=2
My turn! id= 3
count 1
Whoops not my turn, id=3
My turn! id= 0
count 2
Whoops not my turn, id=0
My turn! id= 1
count 2
Whoops not my turn, id=1
My turn! id= 2
count 2
Whoops not my turn, id=2
My turn! id= 3
count 2
Whoops not my turn, id=3
My turn! id= 0
count 3
Whoops not my turn, id=0
My turn! id= 1
count 3
Whoops not my turn, id=1
My turn! id= 2
count 3
Whoops not my turn, id=2
My turn! id= 3
count 3
Whoops not my turn, id=3
My turn! id= 0
count 4
Whoops not my turn, id=0
My turn! id= 1
count 4
Whoops not my turn, id=1
My turn! id= 2
count 4
Whoops not my turn, id=2
My turn! id= 3
count 4
Whoops not my turn, id=3
My turn! id= 0
count 5
Whoops not my turn, id=0
My turn! id= 1
count 5
Whoops not my turn, id=1
My turn! id= 2
count 5
Whoops not my turn, id=2
My turn! id= 3
count 5
Whoops not my turn, id=3
My turn! id= 0
count 6
Whoops not my turn, id=0
My turn! id= 1
count 6
Whoops not my turn, id=1
My turn! id= 2
count 6
Whoops not my turn, id=2
My turn! id= 3
count 6
Whoops not my turn, id=3
My turn! id= 0
count 7
Whoops not my turn, id=0
My turn! id= 1
count 7
Whoops not my turn, id=1
My turn! id= 2
count 7
Whoops not my turn, id=2
My turn! id= 3
count 7
Whoops not my turn, id=3
My turn! id= 0
count 8
Whoops not my turn, id=0
My turn! id= 1
count 8
Whoops not my turn, id=1
My turn! id= 2
count 8
Whoops not my turn, id=2
My turn! id= 3
count 8
Whoops not my turn, id=3
My turn! id= 0
count 9
Whoops not my turn, id=0
My turn! id= 1
count 9
Whoops not my turn, id=1
My turn! id= 2
count 9
Whoops not my turn, id=2
My turn! id= 3
count 9
Whoops not my turn, id=3
My turn! id= 0
count 10
ID=0 has finished
My turn! id= 1
count 10
ID=1 has finished
My turn! id= 2
count 10
ID=2 has finished
My turn! id= 3
count 10
ID=3 has finished

如您所见,每次成功后,线程都会打印“My turn!”,此后它将得到互斥锁,并导致“Whoops not my turn!”。我不明白为什么会在调用pthread_cond_signal时发生这种情况,在当前线程可以重新获取互斥锁之前,pthread_cond_signal应该发出信号通知另一个线程要唤醒。请帮助我找到此解决方案,因为我认为我缺少一些重要的东西。如果缺少我的解释,请随时向我询问更多信息。非常感谢您的宝贵时间!

最佳答案

@rcgldr已经对所涉及的时间给出了很好的解释。如果您想增加给另一个线程一个机会的机会,请尝试添加一个对pthread_yield的调用,这应该使调度程序有机会选择另一个线程,尽管这也不是保证。

关于c - 在具有4个线程的程序上调用pthread_cond_signal时,同一线程将获得互斥体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52656580/

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