这是对这个问题的跟进。
在该代码中,当我不使用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_c
. 似乎也可以使用pthread_mutex_lock();
. 这让我更加困惑。用pthread_mutex_trylock();
too,输出总是这样吗?一个X
接着一个Z
。会不会出现这种情况X
X
Z
X
(假设线程由操作系统切换并且 ythread 尝试锁定并失败)?