gpt4 book ai didi

c - `pthread_mutex_trylock` 和 `pthread_mutex_lock` 行为

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:53:01 25 4
gpt4 key购买 nike

这是对 this 的跟进问题。

在该代码中,当我不使用 fflush(stdout) 时,当我保持 sleep(1) 时,输出不会刷新到屏幕上。

#define S sleep(0)

void* xThread_fn(void* arg)
{
while(1)
{
S;
pthread_mutex_lock(&read_c_mutex);
if(!read_c)
{
pthread_mutex_unlock(&read_c_mutex);
printf(" X");
}
else
{
pthread_mutex_unlock(&read_c_mutex);
pthread_exit(NULL);
}
fflush(stdout); <---THIS ONE HERE
}

}

但是当我保持 sleep(0) 时,就不需要 fflush(stdout),输出会在 stdout 上正确更新。为什么会这样?

Q1.为什么 sleep(0) 的存在会导致刷新输出的方式发生任何变化?

如果我修改代码如下(跟踪执行),

#define S sleep(1)

int read_c = 0;
pthread_mutex_t read_c_mutex = PTHREAD_MUTEX_INITIALIZER;

void* inputThread_fn(void* arg)
{
printf("%p is Input\n",pthread_self());
char inputChar;
int i = 0;
while(1)
{
S;
printf("\nChecking input");
scanf("%c",&inputChar);
if(inputChar=='C' || inputChar == 'c')
{
pthread_mutex_trylock(&read_c_mutex);
printf("%p has lock %d\n",pthread_self(),i);
read_c = 1;
pthread_mutex_unlock(&read_c_mutex);
printf("%p has UNlockED %d\n",pthread_self(),i++);
printf("%p is Gone!\n",pthread_self());
fflush(stdout);
pthread_exit(NULL);
}
}
}

void* xThread_fn(void* arg)
{
int i = 0;
printf("%p is X\n",pthread_self());
while(1)
{
S;
printf("X trying for a lock\n");
pthread_mutex_trylock(&read_c_mutex);
printf("%p has lock %d\n",pthread_self(),i);
if(!read_c)
{
pthread_mutex_unlock(&read_c_mutex);
printf("%p has UNlockED %d\n",pthread_self(),i++);
printf("X\n");
fflush(stdout);
}
else
{
printf("%p is Gone!\n",pthread_self());
pthread_mutex_unlock(&read_c_mutex);
fflush(stdout);
pthread_exit(NULL);
}
}
}

void* yThread_fn(void* arg)
{
printf("%p is Y\n",pthread_self());
int i = 0;
while(1)
{
S;
printf("Y trying for a lock\n");
pthread_mutex_trylock(&read_c_mutex);
printf("%p has lock %d\n",pthread_self(),i);
if(!read_c)
{
pthread_mutex_unlock(&read_c_mutex);
printf("%p has UNlockED %d\n",pthread_self(),i++);
printf("Z\n");
fflush(stdout);
}
else
{
printf("%p is Gone!\n",pthread_self());
pthread_mutex_unlock(&read_c_mutex);
fflush(stdout);
pthread_exit(NULL);
}
}
}

示例输出是

0xb6700b70 is Input
0xb6f01b70 is Y
0xb7702b70 is X

Checking inputY trying for a lock
0xb6f01b70 has lock 0
0xb6f01b70 has UNlockED 0
Z
X trying for a lock
0xb7702b70 has lock 0
0xb7702b70 has UNlockED 0
X
Y trying for a lock
0xb6f01b70 has lock 1
0xb6f01b70 has UNlockED 1
Z
X trying for a lock
0xb7702b70 has lock 1
0xb7702b70 has UNlockED 1
X
Y trying for a lock
0xb6f01b70 has lock 2
0xb6f01b70 has UNlockED 2
Z
X trying for a lock
0xb7702b70 has lock 2
0xb7702b70 has UNlockED 2
X
Y trying for a lock
0xb6f01b70 has lock 3
0xb6f01b70 has UNlockED 3
Z
X trying for a lock
0xb7702b70 has lock 3
0xb7702b70 has UNlockED 3
X
Y trying for a lock
0xb6f01b70 has lock 4
0xb6f01b70 has UNlockED 4
Z
X trying for a lock
0xb7702b70 has lock 4
0xb7702b70 has UNlockED 4
X
c
Y trying for a lock
0xb6f01b70 has lock 5
0xb6f01b70 has UNlockED 5
Z
X trying for a lock
0xb7702b70 has lock 5
0xb7702b70 has UNlockED 5
X
0xb6700b70 has lock 0
0xb6700b70 has UNlockED 0
0xb6700b70 is Gone!
Y trying for a lock
0xb6f01b70 has lock 6
0xb6f01b70 is Gone!
X trying for a lock
0xb7702b70 has lock 6
0xb7702b70 is Gone!

Q2. 我使用了 pthread_mutex_trylock() 因为我希望代码在 while 循环中继续执行,直到它获取一个锁来检查 read_cpthread_mutex_lock(); 似乎也能达到同样的效果。这让我更加困惑。使用pthread_mutex_trylock(); 也是,输出总是这样?一个 X 后跟一个 Z。会不会出现像X X Z X这样的情况(前提是线程切换了由操作系统和 ythread 尝试锁定但失败了)?

最佳答案

回答“trylock”问题。

pthread_mutex_trylock 仅尝试锁定互斥体。如果其他人锁定了互斥锁,它只会返回一个错误并继续运行。由于您没有检查返回值,因此您可能会在不持有互斥锁的情况下触及受互斥锁保护的数据。

您的代码相当于根本没有任何互斥量。

pthread_mutex_trylock 只应在您出于某些非常特殊的原因不能等待锁定并且在获取互斥量失败时回退到不同行为的情况下使用。在不检查返回值的情况下调用它总是一个错误。

为了完全正确,您还应该检查 pthread_mutex_lock 的返回值。但是你通常可以不这样做而逃脱。如果不检查 trylock 的返回值,您将永远无法逃脱。

关于c - `pthread_mutex_trylock` 和 `pthread_mutex_lock` 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15832533/

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