【轉】Linux平台上用C++實現多線程互斥鎖

來源:互聯網
上載者:User

標籤:

原chexlong 原文地址:http://blog.csdn.net/chexlong/article/details/7058283

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

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

Lock.h

[cpp] view plaincopy
  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

[cpp] view plaincopy
  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. }  

 

    測試代碼

[cpp] view plaincopy
  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.     for( int 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. }  

 

    編譯成功後,運行程式

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

[cpp] view plaincopy
  1. //CMyLock lock(g_Lock);  

    運行程式

    結果顯而易見。

【轉】Linux平台上用C++實現多線程互斥鎖

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.