2

在我的代码中,我ACE_Mutex在具有 pthread 的系统 (QNX) 上使用 ACE 库。现在我遇到了一个问题,它的析构函数ACE_Mutex似乎没有调用pthread_mutex_destroy. 当初始化相同内存位置的后续互斥体时,这会带来麻烦,因为pthread_mutex_init返回errno=16( EBUSY)。

ACE_Mutex::remove查看(在Mutex.inl中)的代码,我看到一组奇怪的预编译器指令:

ACE_INLINE int
ACE_Mutex::remove (void)
{
// ACE_TRACE ("ACE_Mutex::remove");
  int result = 0;
#if defined (ACE_HAS_PTHREADS) || defined (ACE_HAS_STHREADS)
  // In the case of a interprocess mutex, the owner is the first
  // process that created the shared memory object. In this case, the
  // lockname_ pointer will be non-zero (points to allocated memory
  // for the name).  Owner or not, the memory needs to be unmapped
  // from the process.  If we are the owner, the file used for
  // shm_open needs to be deleted as well.
  if (this->process_lock_)
    {
      if (this->removed_ == false)
        {
          this->removed_ = true;
          // Only destroy the lock if we're the ones who initialized
          // it.
          if (!this->lockname_)
            ACE_OS::munmap ((void *) this->process_lock_,
                            sizeof (ACE_mutex_t));
          else
            {
              result = ACE_OS::mutex_destroy (this->process_lock_);
              ACE_OS::munmap ((void *) this->process_lock_,
                              sizeof (ACE_mutex_t));
              ACE_OS::shm_unlink (this->lockname_);
              ACE_OS::free (
                static_cast<void *> (
                  const_cast<ACE_TCHAR *> (this->lockname_)));
            }
        }
    }
  else
  {
#else /* !ACE_HAS_PTHREADS && !ACE_HAS_STHREADS */
    if (this->removed_ == false)
      {
        this->removed_ = true;
        result = ACE_OS::mutex_destroy (&this->lock_);
      }
#endif /* ACE_HAS_PTHREADS || ACE_HAS_STHREADS */
#if defined (ACE_HAS_PTHREADS) || defined (ACE_HAS_STHREADS)
  }
#endif /* ACE_HAS_PTHREADS || ACE_HAS_STHREADS */
  return result;
}

具体来说,我不明白为什么调用ACE_OS::mutex_destroy是有条件的,因此在启用 pthread 时不会调用。这有效地使remove非进程间互斥体的方法为空体。有人可以解释这段代码的基本原理吗?

4

0 回答 0