在我的代码中,我在具有pthreads的系统(QNX)上使用ACE库中的ACE_Mutex
。现在我遇到的问题似乎是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
方法为非进程间互斥体呈现空体。有人可以解释这段代码的基本原理吗?