Linux平台用C++封装线程读写锁
来源:互联网 发布:gnuradio ubuntu 编辑:程序博客网 时间:2024/06/04 23:44
RWLockImpl.h view plain #ifndef _RWLockImpl_Header #define _RWLockImpl_Header
#include <iostream> #include <pthread.h> #include <errno.h> #include <assert.h>
using namespace std;
/*读写锁允许当前的多个读用户访问保护资源,但只允许一个写读者访问保护资源*/
//——class CRWLockImpl { protected:CRWLockImpl();~CRWLockImpl();void ReadLockImpl();bool TryReadLockImpl();void WriteLockImpl();bool TryWriteLockImpl();void UnlockImpl();
private:pthread_rwlock_t m_rwl;};
//——
class CMyRWLock: private CRWLockImpl { public:
//创建读/写锁CMyRWLock(){};
//销毁读/写锁~CMyRWLock(){};
//获取读锁//如果其它一个线程占有写锁,则当前线程必须等待写锁被释放,才能对保护资源进行访问void ReadLock();
//尝试获取一个读锁//如果获取成功,则立即返回true,否则当另一个线程占有写锁,则返回false bool TryReadLock();
//获取写锁//如果一个或更多线程占有读锁,则必须等待所有锁被释放//如果相同的一个线程已经占有一个读锁或写锁,则返回结果不确定void WriteLock();
//尝试获取一个写锁//如果获取成功,则立即返回true,否则当一个或更多其它线程占有读锁,返回false //如果相同的一个线程已经占有一个读锁或写锁,则返回结果不确定bool TryWriteLock();
//释放一个读锁或写锁void Unlock();
private:CMyRWLock(const CMyRWLock&);CMyRWLock& operator = (const CMyRWLock&);};
inline void CMyRWLock::ReadLock()
{ ReadLockImpl();}
inline bool CMyRWLock::TryReadLock()
{ return TryReadLockImpl();}
inline void CMyRWLock::WriteLock()
{ WriteLockImpl();}
inline bool CMyRWLock::TryWriteLock()
{ return TryWriteLockImpl();}
inline void CMyRWLock::Unlock()
{ UnlockImpl();}
#endif RWLockImpl.cpp view plain #include "RWLockImpl.h"
CRWLockImpl::CRWLockImpl()
{ if (pthread_rwlock_init(&m_rwl, NULL))
cout<<"cannot create reader/writer lock"<<endl;}
CRWLockImpl::~CRWLockImpl()
{ pthread_rwlock_destroy(&m_rwl);}
void CRWLockImpl::ReadLockImpl()
{ if (pthread_rwlock_rdlock(&m_rwl))
cout<<"cannot lock reader/writer lock"<<endl;}
bool CRWLockImpl::TryReadLockImpl()
{ int rc = pthread_rwlock_tryrdlock(&m_rwl);if (rc == 0)
return true;else if (rc == EBUSY)
return false;else cout<<"cannot lock reader/writer lock"<<endl;
return false;}
void CRWLockImpl::WriteLockImpl()
{ if (pthread_rwlock_wrlock(&m_rwl))
cout<<"cannot lock reader/writer lock"<<endl;}
bool CRWLockImpl::TryWriteLockImpl()
{ int rc = pthread_rwlock_trywrlock(&m_rwl);if (rc == 0)
return true;else if (rc == EBUSY)
return false;else cout<<"cannot lock reader/writer lock"<<endl;return false;}
void CRWLockImpl::UnlockImpl()
{ if (pthread_rwlock_unlock(&m_rwl))
cout<<"cannot unlock reader/writer lock"<<endl;}
下边是测试代码view plain // pthread_rwlock.cpp : 定义控制台应用程序的入口点。
//
#include "RWLockImpl.h"
//创建一个读写锁对象CMyRWLock g_myRWLock;volatile int g_counter = 0;
//线程函数void * StartThread(void *pParam)
{ int lastCount = 0;for (int i = 0; i < 10000; ++i)
{ g_myRWLock.ReadLock();lastCount = g_counter;//在读锁域,两个线程不断循环交替访问全局变量g_counter for (int k = 0; k < 100; ++k)
{ if (g_counter != lastCount)
cout<<"the value of g_counter has been updated."<<endl;sleep(0);} g_myRWLock.Unlock();
g_myRWLock.WriteLock();//在写锁域,只有一个线程可以修改全局变量g_counter的值for (int k = 0; k < 100; ++k)
{——g_counter;sleep(0);} for (int k = 0; k < 100; ++k)
{ ++g_counter;sleep(0);} ++g_counter;if (g_counter <= lastCount)
cout<<"the value of g_counter is error."<<endl;g_myRWLock.Unlock();}
return (void *)0;}
int main(int argc, char* argv[])
{ pthread_t thread1,thread2;pthread_attr_t attr1,attr2;
//创建两个工作线程pthread_attr_init(&attr1);pthread_attr_setdetachstate(&attr1,PTHREAD_CREATE_JOINABLE);if (pthread_create(&thread1,&attr1, StartThread,0) == -1)
{ cout<<"Thread 1: create failed"<<endl;} pthread_attr_init(&attr2);pthread_attr_setdetachstate(&attr2,PTHREAD_CREATE_JOINABLE);if (pthread_create(&thread2,&attr2, StartThread,0) == -1)
{ cout<<"Thread 2: create failed"<<endl;}
//等待线程结束void *result;pthread_join(thread1,&result);pthread_join(thread2,&result);
//关闭线程,释放资源pthread_attr_destroy(&attr1);pthread_attr_destroy(&attr2);
cout<<"the g_counter = "<<g_counter<<endl;
int iWait;cin>>iWait;
return 0;}
编译,运行
运行结果与在Win32下用C++实现多线程读写锁的相同。
- Linux平台用C++封装线程读写锁
- (转)Linux平台用C++封装线程读写锁
- Linux平台用C++封装线程读写锁
- Linux平台用C++封装线程读写锁
- Linux平台用C++封装线程读写锁
- Linux平台用C++封装线程读写锁
- C语言读写BMP linux 平台
- linux平台使用读写锁
- c印记(十四):跨平台线程封装
- Linux线程的读写锁
- Linux线程同步-----读写锁
- linux编程---线程---读写锁
- Linux线程同步(3): 读写锁(rwlock)
- Linux线程同步之读写锁
- Linux线程同步(3): 读写锁(rwlock)
- Linux线程同步(3): 读写锁(rwlock)
- Linux 下的线程读写锁
- Linux 下的线程读写锁
- Java当中的内存分配以及值传递问题内存解析
- 用Google Scholar输出文献bibtex
- 重新开始体验 Web
- JAVA内存模型
- 东芝超强悍的Windows 8 平板
- Linux平台用C++封装线程读写锁
- varnish缓存清除的问题和陷阱
- [转载]最好的53个 VC++ /MFC 开源软件项目
- loadrunner脚本中写入脚本输出log到外部文件,分析参数取值方式
- CSS3多列技术简单介绍
- GBIF 资源介绍 IPT等
- macpro sqlite tool
- 事务特性: 原子性、一致性、分离性、持久性
- iPad应用的十大用户体验设计准则