0

这是对这个问题的跟进。

在该代码中,当我不使用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 尝试锁定并失败)?

4

2 回答 2

4

回答“trylock”问题。

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

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

pthread_mutex_trylock仅应在由于某些非常特殊的原因而无法等待锁定并且在获取互斥锁失败时将退回到不同行为的情况下使用。在不检查返回值的情况下调用它始终是一个错误。

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

于 2013-04-05T11:51:38.763 回答
1

当调用 sleep 时,进程被上下文切换(调度)。sleep(0) 表示你并没有真正在睡觉。所以进程继续执行。Printf 使用缓冲的标准输出。因此,您打印的任何内容都会进入该输出缓冲区,然后刷新到终端(屏幕)上。当进程休眠时,缓冲区不会被刷新,因此除非您进行显式刷新,否则您不会获得输出。如果您不休眠,则进程有机会执行刷新缓冲区并立即获得输出。每当您在程序中使用任何阻塞调用或休眠行为时,您都会发现 printf 的这种行为。如果您使用 fprintf(stderr,"your string") 那么无论您是否睡觉,它都会始终打印。

于 2013-04-05T13:00:41.827 回答