2012-08-16 62 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