是否有必要在互斥锁上调用pthread

是否有必要在互斥锁上调用pthread

本文介绍了是否有必要在互斥锁上调用pthread_mutex_destroy?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在C ++程序中使用pthread_mutex_t,如下所示:

class Mutex : public noncopyable
{
public:
    Mutex()
    {
        pthread_mutex_init(&m_mutex, NULL);
    }

    void acquire()
    {
        pthread_mutex_lock(&m_mutex);
    }

    void release()
    {
        pthread_mutex_unlock(&m_mutex);
    }

private:
    pthread_mutex_t m_mutex;
};

(该类不可复制- http://www.boost .org/doc/libs/1_53_0/boost/noncopyable.hpp )

我不了解的事情-在析构函数中调用 not 调用pthread_mutex_destroy是否被视为错误?我阅读的文档没有说明必须调用destroy.

有人知道吗,pthread_mutex_destroy实际上是做什么的,在什么条件下需要?

编辑

pthread_mutex_destroy的答案也适用于pthread_cond_destroy等吗?除非pthread_mutex_init等,它们对我来说几乎像是无用的功能. al.正在分配内存? (对我而言,文档尚不完全清楚.)

无论如何称呼销毁并没有伤害我,所以问题主要是学术上的.

无论如何,在Linux上,destroy只会将互斥锁设置为无效状态:

int
__pthread_mutex_destroy (mutex)
     pthread_mutex_t *mutex;
{
  if ((mutex->__data.__kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP) == 0
      && mutex->__data.__nusers != 0)
    return EBUSY;

  /* Set to an invalid value.  */
  mutex->__data.__kind = -1;

  return 0;
}

(来自glibc-2.14/nptl/pthread_mutex_destroy.c).

解决方案

如果有人为您提供了destroy函数,那么您必须在对象超出范围之前将其作为对该对象的最终操作来调用.

在API不起作用的体系结构和实现上,将对其进行优化,但是,如果API将来更改以要求清除内部状态并且您的代码未调用它,则您的代码现在将具有一个内存并/或资源泄漏.

所以简单的答案是肯定的.您必须调用此API-事情就是这样-即使该API目前没有任何作用,因为尽管该API本身已在将来永久固定,但该API的实现却并非如此. >

I am using pthread_mutex_t in a C++ program, as follows:

class Mutex : public noncopyable
{
public:
    Mutex()
    {
        pthread_mutex_init(&m_mutex, NULL);
    }

    void acquire()
    {
        pthread_mutex_lock(&m_mutex);
    }

    void release()
    {
        pthread_mutex_unlock(&m_mutex);
    }

private:
    pthread_mutex_t m_mutex;
};

(The class is not copyable - http://www.boost.org/doc/libs/1_53_0/boost/noncopyable.hpp)

The thing that I don't understand - is it considered an error to not call pthread_mutex_destroy in the destructor? The documentation I have read does not state that destroy must be called.

Does anyone know, what does pthread_mutex_destroy actually do and under what conditions is it required?

EDIT

Does the answer for pthread_mutex_destroy also apply to pthread_cond_destroy, etc? They seem almost like useless functions to me, unless pthread_mutex_init et. al. are allocating memory? (the docs, to me, aren't entirely clear on this.)

It doesn't hurt me to call destroy anyway, so the question is largely academic.

On linux anyway, it seems destroy only sets the mutex to an invalid state:

int
__pthread_mutex_destroy (mutex)
     pthread_mutex_t *mutex;
{
  if ((mutex->__data.__kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP) == 0
      && mutex->__data.__nusers != 0)
    return EBUSY;

  /* Set to an invalid value.  */
  mutex->__data.__kind = -1;

  return 0;
}

(From glibc-2.14/nptl/pthread_mutex_destroy.c).

解决方案

If someone provides you with a destroy function, then you are required to call it as the final action on that object before it goes out of scope.

On architectures and implementations where the API has no effect, this will be optimised away, however if the API changes in future to require cleaning up of internal state and your code does not call it, your code will now have a memory and/or resource leak.

So the simple answer is yes; you must call this API - and here's the thing - even if the API does nothing at the moment, because although the API itself is fixed forever into the future, the implementation behind the API is not.

这篇关于是否有必要在互斥锁上调用pthread_mutex_destroy?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 17:25