您的位置:首页 > 运维架构 > Linux

Linux平台上用C++实现多线程互斥锁

2012-06-19 13:19 585 查看
在上篇用C++实现了Win32平台上的多线程互斥锁( http://www.linuxidc.com/Linux/2011-12/49713.htm ),这次写个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.}

本篇文章来源于 Linux公社网站(www.linuxidc.com)  原文链接:http://www.linuxidc.com/Linux/2011-12/49714.htm

 

测试代码

 

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.} 

 

 

    编译成功后,运行程序

 

同样,若将下边代码注释掉,重新编译

 

1.//CMyLock lock(g_Lock);  

 

    运行程序

 

 

结果显而易见。

本篇文章来源于 Linux公社网站(www.linuxidc.com)  原文链接:http://www.linuxidc.com/Linux/2011-12/49714p2.htm

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息