Linux平臺上用C++實現多線程互斥鎖

http://blog.csdn.net/chexlong/article/details/7058283

  在上篇用C++實現了Win32平臺上的多線程互斥鎖,這次寫個Linux平臺上的,同樣參考了開源項目C++ Sockets的代碼,在此對這些給開源項目做出貢獻的鬥士們表示感謝!

    下邊分別是互斥鎖類和測試代碼,已經在Fedora 13虛擬機上測試通過。

Lock.h

  1. #ifndef _Lock_H  
  2. #define _Lock_H  
  3.   
  4. #include <pthread.h>  
  5.   
  6. //鎖接口類  
  7. class ILock  
  8. {  
  9. public:  
  10.     virtual ~ILock() {}  
  11.   
  12.     virtual void Lock() const = 0;  
  13.     virtual void Unlock() const = 0;  
  14. };  
  15.   
  16. //互斥鎖類  
  17. class CMutex : public ILock  
  18. {  
  19. public:  
  20.     CMutex();  
  21.     ~CMutex();  
  22.   
  23.     virtual void Lock() const;  
  24.     virtual void Unlock() const;  
  25.   
  26. private:  
  27.     mutable pthread_mutex_t m_mutex;  
  28. };  
  29.   
  30. //鎖  
  31. class CMyLock  
  32. {  
  33. public:  
  34.     CMyLock(const ILock&);  
  35.     ~CMyLock();  
  36.   
  37. private:  
  38.     const ILock& m_lock;  
  39. };  
  40.   
  41.   
  42. #endif  

Lock.cpp

  1. #include "Lock.h"  
  2.   
  3.   
  4. //動態方式初始化互斥鎖  
  5. CMutex::CMutex()  
  6. {  
  7.     pthread_mutex_init(&m_mutex, NULL);  
  8. }  
  9.   
  10. //註銷互斥鎖  
  11. CMutex::~CMutex()  
  12. {  
  13.     pthread_mutex_destroy(&m_mutex);  
  14. }  
  15.   
  16. //確保擁有互斥鎖的線程對被保護資源的獨自訪問  
  17. void CMutex::Lock() const  
  18. {  
  19.     pthread_mutex_lock(&m_mutex);  
  20. }  
  21.   
  22. //釋放當前線程擁有的鎖,以使其它線程可以擁有互斥鎖,對被保護資源進行訪問  
  23. void CMutex::Unlock() const  
  24. {  
  25.     pthread_mutex_unlock(&m_mutex);  
  26. }  
  27.   
  28. //利用C++特性,進行自動加鎖  
  29. CMyLock::CMyLock(const ILock& m) : m_lock(m)  
  30. {  
  31.     m_lock.Lock();  
  32. }  
  33.   
  34. //利用C++特性,進行自動解鎖  
  35. CMyLock::~CMyLock()  
  36. {  
  37.     m_lock.Unlock();  
  38. }  

 

    測試代碼

  1. // pthread_mutex.cpp : 定義控制檯應用程序的入口點。  
  2. //  
  3.   
  4. #include <iostream>  
  5. #include <unistd.h>  
  6. #include "Lock.h"  
  7.   
  8. using namespace std;  
  9.   
  10. //創建一個互斥鎖  
  11. CMutex g_Lock;  
  12.   
  13.   
  14. //線程函數  
  15. void * StartThread(void *pParam)  
  16. {  
  17.     char *pMsg = (char *)pParam;  
  18.     if (!pMsg)  
  19.     {  
  20.         return (void *)1;  
  21.     }  
  22.   
  23.     //對被保護資源(以下打印語句)自動加鎖  
  24.     //線程函數結束前,自動解鎖  
  25.     CMyLock lock(g_Lock);  
  26.   
  27.     forint i = 0; i < 5; i++ )  
  28.     {  
  29.         cout << pMsg << endl;  
  30.         sleep( 1 );  
  31.     }  
  32.   
  33.     return (void *)0;  
  34. }  
  35.   
  36. int main(int argc, char* argv[])  
  37. {  
  38.     pthread_t thread1,thread2;  
  39.     pthread_attr_t attr1,attr2;  
  40.   
  41.     char *pMsg1 = "First print thread.";  
  42.     char *pMsg2 = "Second print thread.";  
  43.   
  44.     //創建兩個工作線程,分別打印不同的消息  
  45.     pthread_attr_init(&attr1);  
  46.     pthread_attr_setdetachstate(&attr1,PTHREAD_CREATE_JOINABLE);  
  47.     if (pthread_create(&thread1,&attr1, StartThread,pMsg1) == -1)  
  48.     {  
  49.         cout<<"Thread 1: create failed"<<endl;  
  50.     }  
  51.     pthread_attr_init(&attr2);  
  52.     pthread_attr_setdetachstate(&attr2,PTHREAD_CREATE_JOINABLE);  
  53.     if (pthread_create(&thread2,&attr2, StartThread,pMsg2) == -1)  
  54.     {  
  55.         cout<<"Thread 2: create failed"<<endl;  
  56.     }  
  57.   
  58.     //等待線程結束  
  59.     void *result;  
  60.     pthread_join(thread1,&result);  
  61.     pthread_join(thread2,&result);  
  62.   
  63.     //關閉線程,釋放資源  
  64.     pthread_attr_destroy(&attr1);  
  65.     pthread_attr_destroy(&attr2);  
  66.   
  67.     int iWait;  
  68.     cin>>iWait;  
  69.   
  70.     return 0;  
  71. }  


 

    編譯成功後,運行程序

    同樣,若將下邊代碼註釋掉,重新編譯

  1. //CMyLock lock(g_Lock);  

    運行程序


    結果顯而易見。

 


發佈了20 篇原創文章 · 獲贊 7 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章