ACE_Mutex从不调用pthread_mutex_destroy

时间:2012-11-30 13:58:29

标签: c++ pthreads ace

在我的代码中,我在具有pthreads的系统(QNX)上使用ACE库中的ACE_Mutex。现在我遇到的问题似乎是ACE_Mutex的析构函数没有调用pthread_mutex_destroy。这会在初始化同一内存位置的后续互斥锁时出现问题,因为pthread_mutex_init会返回errno=16EBUSY)。

查看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方法为非进程间互斥体呈现空体。有人可以解释这段代码的基本原理吗?

0 个答案:

没有答案