不同的互斥锁属性

时间:2013-11-16 20:51:25

标签: c pthreads mutex

函数pthread_mutexattr_settype是否与pthread_mutexattr_setkind_np不同? 我有随机输出,我不确定... 我试图看看是否发生了什么,如果我键入正常,错误检查和递归类型的互斥...

switch(atoi(argv[1])){
    case 1:
      puts("ERROR CHECK");
      pthread_mutexattr_settype(&m_attr, PTHREAD_MUTEX_ERRORCHECK);
      break;
    case 2:
      puts("RECURSIVE");
      pthread_mutexattr_settype(&m_attr, PTHREAD_MUTEX_RECURSIVE);
      break;
    default:
      puts("NORMAL");
      pthread_mutexattr_settype(&m_attr, PTHREAD_MUTEX_NORMAL);
      break;  
  }

puts("Before 1 lock");
pthread_mutex_lock(&data->mutex);
test_errno("1 lock");
puts("          After 1 lock");

puts("Before 2 lock");
pthread_mutex_lock(&data->mutex);
test_errno("2 lock");
puts("      After 2 lock");

puts("Before 1 unlock");
pthread_mutex_unlock(&data->mutex);
test_errno("1 unlock");
puts("      After 1 ulock");

puts("Before 2 unlock");
pthread_mutex_unlock(&data->mutex);
test_errno("2 unlock");
puts("      After 2 ulock");

1 个答案:

答案 0 :(得分:2)

如果查看源代码(ftp://sourceware.org/pub/pthreads-win32/sources/pthreads-w32-2-9-1-release/pthread_mutexattr_setkind_np.c),您会发现以下内容:

int
pthread_mutexattr_setkind_np (pthread_mutexattr_t * attr, int kind)
{
  return pthread_mutexattr_settype (attr, kind);
}

除了名称之外,这两种方法也具有完全相同的签名,因此您可以依赖它们彼此的别名。手册页https://sourceware.org/pthreads-win32/manual/pthread_mutexattr_init.html还指出pthread_mutexattr_setkind_nppthread_mutexattr_settype的别名。