`pthread_mutex_trylock`和`pthread_mutex_lock`行为

时间:2013-04-05 11:17:18

标签: c linux multithreading pthreads

这是对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_c的值。使用pthread_mutex_lock();似乎也是如此。这让我更加困惑。使用pthread_mutex_trylock(); ,输出总是这样?一个X后跟一个Z。不会出现类似X X Z X的情况(假设线程由操作系统切换而ythread尝试锁定并失败) ?

2 个答案:

答案 0 :(得分:4)

回答“trylock”问题。

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

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

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

要完全正确,您还应该检查pthread_mutex_lock的返回值。但是你通常可以逃避不这样做。你永远不会忘记没有检查trylock的返回值。

答案 1 :(得分:1)

当调用sleep时,进程被上下文切换出来(预定出)。 睡觉(0)意味着你没有真正睡觉。因此,流程继续执行。 Printf使用缓冲的stdout。因此,无论您打印什么,都会进入该输出缓冲区,然后再刷新到终端(屏幕)上。 当进程休眠缓冲区没有被刷新时,除非你进行显式刷新,否则你不会得到输出。如果你没有睡眠进程有机会做冲洗缓冲区并立即获得输出。 无论何时在程序中使用任何阻塞调用或休眠行为,都会发现printf的这种行为。如果你使用fprintf(stderr,“你的字符串”),无论你是否睡觉,它都会打印出来。