在pthread_mutex_init之前调用pthread_mutex_lock是否安全?

时间:2015-07-27 21:54:19

标签: c pthreads mutex

我之前从未有机会使用pthreads库,但我正在审查一些涉及pthread互斥体的代码。我查看了try { int x= 2048; TcpClient tcpclnt = new TcpClient(); tcpclnt.Connect("localhost", 8091); // use the ipaddress as in the server program Stream stm = tcpclnt.GetStream(); stm.ReadTimeout = 2000; byte[] bb = new byte[x]; int k = stm.Read(bb, 0, x); for (int i = 0; i < k; i++) { richTextBox2.AppendText(Convert.ToString(Convert.ToChar(bb[i]))); } tcpclnt.Close(); } catch ( Exception y ) { Console.WriteLine("Error..... " + y.StackTrace); } pthread_mutex_lock的文档,我在阅读这两个函数的手册页时的理解是,在致电pthread_mutex_init之前,我必须致电pthread_mutex_init

但是,我问了几位同事,他们认为在致电pthread_mutex_lock之前拨打pthread_mutex_lock是可以的。我正在审核的代码也调用了pthread_mutex_init,甚至没有调用pthread_mutex_lock

基本上,在调用pthread_mutex_init之前调用pthread_mutex_lock是否安全且明智(如果pthread_mutex_init甚至被调用)?

编辑:我还看到一些示例,其中pthread_mutex_init在未使用pthread_mutex_lock时被调用,例如this example

编辑#2:这是我正在审查的代码。请注意,configure函数获取并附加到某些未初始化的共享内存。稍后的Java代码将调用pthread_mutex_init,而不会在其间调用其他本机函数。 Link to code

3 个答案:

答案 0 :(得分:7)

POSIX standard说:

  

如果out = new FileOutputStream(f); 未引用已初始化的互斥对象,则表示该行为   mutexpthread_mutex_lock()和。{   pthread_mutex_trylock()未定义。

所以你需要初始化互斥锁。这可以通过致电pthread_mutex_unlock()来完成;或者,如果互斥锁具有静态存储持续时间,则使用静态初始化程序pthread_mutex_init()。例如:

PTHREAD_MUTEX_INITIALIZER

答案 1 :(得分:6)

互斥锁是包含函数需要完成其工作的状态(信息)的变量。如果不需要任何信息,例程就不需要变量。同样,如果您向其提供随机垃圾,例程可能无法正常运行。

大多数平台都接受填充零字节的互斥对象。这通常是pthread_mutex_initPTHREAD_MUTEX_INITIALIZER创建的内容。碰巧的是,C语言还保证在程序启动时将未初始化的全局变量清零。因此,您可能看起来不需要初始化pthread_mutex_t个对象,但事实并非如此。特别是生活在堆栈或堆中的东西通常不会被归零。

pthread_mutex_init之后调用pthread_lock 肯定会产生不良后果。它会覆盖变量。可能的结果:

  1. 互斥锁解锁。
  2. 另一个线程尝试获取锁定的竞争条件,导致崩溃。
  3. 资源泄露在库或内核中(但在进程终止时将被释放)。

答案 2 :(得分:1)

这是我在评论中发布的链接中的文字:

 Mutual exclusion locks (mutexes)  prevent  multiple  threads
 from simultaneously executing critical sections of code that
 access shared data (that is, mutexes are used  to  serialize
 the  execution  of  threads).  All mutexes must be global. A
 successful call for a mutex lock  by  way  of   mutex_lock()
 will  cause  another  thread that is also trying to lock the
 same mutex to block until the owner thread unlocks it by way
 of   mutex_unlock().  Threads  within  the  same  process or
 within other processes can share mutexes.

 Mutexes can synchronize threads within the **same  process**  or
 in  ***other   processes***.  Mutexes  can  be used to synchronize
 threads between processes if the mutexes  are  allocated  in
 writable  memory  and shared among the cooperating processes
 (see mmap(2)), and have been initialized for this task.

Initialize Mutexes are either intra-process or inter-process,
depending upon the argument passed implicitly or explicitly to the initialization of that mutex.
A statically allocated mutex does not need to be explicitly initialized;
by default, a statically allocated mutex is initialized with all zeros and its scope is set to be within the calling process.

 For inter-process synchronization, a mutex needs to be allo-
 cated   in  memory shared between these processes. Since the
 memory for such a mutex must be allocated dynamically,   the
 mutex needs to be explicitly initialized using mutex_init().




also, for inter-process synchronization,
besides the requirement to be allocated in shared memory,
the mutexes must also use the attribute PTHREAD_PROCESS_SHARED,
otherwise accessing the mutex from another process than its creator results in undefined behaviour
(see this: linux.die.net/man/3/pthread_mutexattr_setpshared):

The process-shared attribute is set to PTHREAD_PROCESS_SHARED to permit a
mutex to be operated upon by any thread that has access to the memory
where the mutex is allocated, even if the mutex is allocated in memory that is shared by multiple processes