c - 不同的互斥属性

标签 c pthreads mutex

函数 pthread_mutexattr_settypepthread_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");

最佳答案

如果您查看源代码 ( 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 的别名。

关于c - 不同的互斥属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20023730/

相关文章:

c - 标准C库的正则表达式匹配函数

c - 在 #ifdef 中使用多个术语

c++ - 从 std::thread 到相关的 pthread

c# - 为什么这个程序会出错? `Object synchronization method was called from an unsynchronized block of code`

c - 查看gdb中的寄存器在流程c之后没有改变

c - C中的猜谜游戏,无法获得有效的尝试次数,变量为 "counter"

c - 为什么 pthread_cond_signal 有时不起作用?

php - 如何计算 PHP 脚本(linux)中的处理器内核数?

c - 在c中的进程间同步中互斥体的使用

Python多处理安全地写入文件