2012-08-16 68 views
3

这是我迄今所做的:创建在C互斥锁柜类++

class mutexLocker 
{ 
    private: 
    /* Declaration of a Mutex variable `mutexA`. */ 
    pthread_mutex_t &mutexA; 

    /* `mutexStatus` holds the return value of the function`pthread_mutex_lock `. 
    This value has to be returned to the callee so we need to preserve it in a class 
    variable. */ 
    int    mutexStatus; 

    /* We need to decide how to deal with the situation when one thread tries to lock then 
    mutex repeatedly. If the mutex is of type recursive, then there won't be any problem for 
    sure, but otherwise the case may result in a deadlock. */ 
    pthread_t  calleeThreadId; 

    public: 
    /* Constructor attempts to lock the desired mutex variable. */ 
    mutexLocker (pthread_mutex_t argMutexVariable, pthread_t threadId) 
    : mutexA (argMutexVariable, calleeThreadId) 
    { 
     /* Return value is needed in order to know whether the mutex has been 
     successfully locked or not. */ 
     int mutexStatus = pthread_mutex_lock (&argMutexVariable); 
    } 

    /* Since the constructor can't return anything, we need to have a separate function 
    which returns the status of the lock. */ 
    int getMutexLockStatus() 
    { 
     return mutexStatus; 
    } 

    /* The destructor will get called automatically whereever the callee's scope ends, and 
    will get the mutex unlocked. */ 
    ~mutexLocker() 
    { 
     pthread_mutex_unlock (&mutexA); 
    } 
}; 

应在DIY互斥锁柜类中提供什么其他功能?

+0

是否有你想实现自己的原因而不是使用Boost或C++ 11标准库中可用的锁类? – 2012-08-16 06:10:11

+2

“DIY互斥锁类中应该提供哪些其他功能?”是一个非常主观的问题,“可能会争辩辩论,辩论,投票或扩大讨论” - 因此投票结束时不具有建设性。 – paxdiablo 2012-08-16 06:11:52

+1

@JoachimPileborg我不知道C++标准有任何互斥锁类!我以前在此搜索过Google。我会再看看。 – 2012-08-16 06:14:19

回答

5

我完全Slavik81的评论表示赞同没有创造功能,你不必为使用情况。

尽管如此引用锁类新的升压实现可以是在理解它们的接口的共同需求方面的良好起点:http://www.boost.org/doc/libs/1_42_0/doc/html/thread/synchronization.html#thread.synchronization.locks

在标准C++ 11方面介绍std::lock_guardhttp://en.cppreference.com/w/cpp/thread/lock_guard

+0

感谢您的联系,我会期待。但重建这个学习不好吗?将重建这不会有助于理解线程和所有的基础知识吗? – 2012-08-16 06:51:53

+0

为了你自己的理解,写一个没有什么坏处,但除此之外,如果你使用C++ 11,通常会坚持使用'std :: lock_guard',否则'boost :: lock_guard'因为它经过了充分测试和普遍理解。 – Graeme 2012-08-16 06:55:24

5

你向后接近这一点。你编写代码来解决你面临的问题。不要编写代码来解决你没有的问题。 “You aren't gonna need it”是一个很好的原则。

除非您有可以解决的问题,否则不应提供其他功能。鉴于你没有提到过,我假设你没有这个问题。因此,还没有其他功能应该添加。

1

首先,Graeme和Slavik81的答案都非常好(+1)。

现在,至于什么补充:

  • 可能要附加误差的支持,这取决于你如何处理错误。例如,如果您的团队在锁定失败时抛出异常,则该类可能会创建并抛出异常。
  • 作为诊断,您可能要验证客户端测试的采集(在析构函数例如assert如果他们从来没有证实该锁成功)成功了。
  • 你可能想要一个'重试'获取功能。在抛出异常之前重试更好,imo。就个人而言,如果EBUSY - 我不认为它是一个程序员的错误 - 不要锁定长锁!
  • 也将你的pthread_mutex_t藏在一个类中 - 禁止复制,赋值等。把它传给你的储物柜。
  • 一些客户可能想要一个简单的方法来测试成功,而不是评估状态。取决于你如何使用它。
  • 如果您没有获取锁,请勿在dtor解锁
  • 检查解锁的结果。确定在这种情况下的错误处理。

也许更重要的是 - 确定要删除

  • 不可复印。明确删除拷贝表达意图。
  • 删除operator= - 表示意图。
  • 删除移动
  • calleeThreadId删除,除非您发现有用。具体来说,考虑你将如何实际实现你提出的错误检测 - 储物柜似乎是数据的错误位置,imo。我宁愿保持这些储物柜最小,集中和简单。
  • 额外一英里:防止堆分配;例如删除operator new/delete的主变体。
  • 如果您不重试,则状态可能为const
  • threadID,如果你决定保留它,也可能是const

最后,通过引用中的互斥量(虽然我认为它是一个错字)。

+0

你说:*“calleeThreadId - 删除,除非你觉得有用。”*我认为这将有助于检测是否有递归调用非递归互斥体。如果你能详细解释其余的问题,我将不胜感激。你的意思是我不应该将函数参数互斥体复制到类变量互斥体? – 2012-08-16 07:08:08

+0

@AnishaKaul ok - 扩大 – justin 2012-08-16 07:33:23

+0

但如果我们安全地复制/分配,会出现什么问题?我没有安全地做过吗? – 2012-08-16 07:36:46