利用boost::mutex实现线程安全列表模板类,附源码及使用例子
2010-05-13 09:47
471 查看
std::list本身不具备线程安全功能,本文利用开源库boost的互斥体类mutex实现一个带互斥功能的线程安全列表模板类。
1 [/b]定义一个模板类,继承于[/b]std::list[/b],增加[/b]boost::mutex[/b]成员变量,代码如下:[/b][/b]
#include "list"
#include <boost/thread.hpp>
template<typename T>
class CLockList
: public std::list<T>
{
protected:
boost::mutex m_mutex;
};
2 [/b]我们主要实现[/b]add[/b]、[/b]front[/b]和[/b]clear[/b]三个函数,带互斥功能,其他功能函数可以自行添加;[/b][/b]
add[/b]函数实现把元素添加到列表末尾,代码如下:[/b]
void add(T t)
{
boost::mutex::scoped_lock lock(m_mutex);
std::list<T>::push_back(t);
}
front[/b]函数实现取出,列表头元素,is_pop[/b]决定是否删除该元素,代码如下:[/b]
bool front(T & out, bool is_pop = true)
{
boost::mutex::scoped_lock lock(m_mutex);
typename std::list<T>::iterator pIter = std::list<T>::begin();
if (pIter == std::list<T>::end())
return false;
out = *pIter;
if (is_pop)
std::list<T>::pop_front();
return true;
}
clear[/b]函数实现清空该列表,is_lock[/b]决定是否加锁,可以适应不同的使用环境,代码如下:[/b]
void clear(bool is_lock = true)
{
if (is_lock)
{
boost::mutex::scoped_lock lock(m_mutex);
std::list<T>::clear();
}else
{
std::list<T>::clear();
}
}
[/b]3 [/b]最后记得在模板类的析构函数位置加上[/b]clear[/b]函数,保证退出时释放资源,全部代码如下:[/b][/b]
// locklist.h file here
#ifndef __locklist_h__
#define __locklist_h__
//
#include "list"
#include "stldef.h"
#include <algorithm>
#include <boost/thread.hpp>
template<typename T>
class CLockList
: public std::list<T>
{
protected:
boost::mutex m_mutex;
public:
boost::mutex & mutex(void) {return m_mutex;}
const boost::mutex & mutex(void) const {return m_mutex;}
void add(T t)
{
boost::mutex::scoped_lock lock(m_mutex);
std::list<T>::push_back(t);
}
bool front(T & out, bool is_pop = true)
{
boost::mutex::scoped_lock lock(m_mutex);
typename std::list<T>::iterator pIter = std::list<T>::begin();
if (pIter == std::list<T>::end())
return false;
out = *pIter;
if (is_pop)
std::list<T>::pop_front();
return true;
}
void clear(bool is_lock = true)
{
if (is_lock)
{
boost::mutex::scoped_lock lock(m_mutex);
std::list<T>::clear();
}else
{
std::list<T>::clear();
}
}
public:
CLockList(void)
{
}
~CLockList(void)
{
clear();
}
};
#endif // __locklist_h__
4 [/b]简单使用例子,假设有一个用户信息类[/b]CUserInfo[/b],基本使用例子如下:[/b][/b]
int main(void)
{
// 定义用户列表
CLockList<CUserInfo> m_users;
// 用户信息实例
CUserInfo user1;
CUserInfo user2;
// 添加
m_users.add(user1);
m_users.add(user2);
// 循环取出
CUserInfo userInfo;
while (m_users.front(userInfo))
{
// ...
}
// 在某些场合需要提前清空列表;
m_users.clear();
return 0;
}
1 [/b]定义一个模板类,继承于[/b]std::list[/b],增加[/b]boost::mutex[/b]成员变量,代码如下:[/b][/b]
#include "list"
#include <boost/thread.hpp>
template<typename T>
class CLockList
: public std::list<T>
{
protected:
boost::mutex m_mutex;
};
2 [/b]我们主要实现[/b]add[/b]、[/b]front[/b]和[/b]clear[/b]三个函数,带互斥功能,其他功能函数可以自行添加;[/b][/b]
add[/b]函数实现把元素添加到列表末尾,代码如下:[/b]
void add(T t)
{
boost::mutex::scoped_lock lock(m_mutex);
std::list<T>::push_back(t);
}
front[/b]函数实现取出,列表头元素,is_pop[/b]决定是否删除该元素,代码如下:[/b]
bool front(T & out, bool is_pop = true)
{
boost::mutex::scoped_lock lock(m_mutex);
typename std::list<T>::iterator pIter = std::list<T>::begin();
if (pIter == std::list<T>::end())
return false;
out = *pIter;
if (is_pop)
std::list<T>::pop_front();
return true;
}
clear[/b]函数实现清空该列表,is_lock[/b]决定是否加锁,可以适应不同的使用环境,代码如下:[/b]
void clear(bool is_lock = true)
{
if (is_lock)
{
boost::mutex::scoped_lock lock(m_mutex);
std::list<T>::clear();
}else
{
std::list<T>::clear();
}
}
[/b]3 [/b]最后记得在模板类的析构函数位置加上[/b]clear[/b]函数,保证退出时释放资源,全部代码如下:[/b][/b]
// locklist.h file here
#ifndef __locklist_h__
#define __locklist_h__
//
#include "list"
#include "stldef.h"
#include <algorithm>
#include <boost/thread.hpp>
template<typename T>
class CLockList
: public std::list<T>
{
protected:
boost::mutex m_mutex;
public:
boost::mutex & mutex(void) {return m_mutex;}
const boost::mutex & mutex(void) const {return m_mutex;}
void add(T t)
{
boost::mutex::scoped_lock lock(m_mutex);
std::list<T>::push_back(t);
}
bool front(T & out, bool is_pop = true)
{
boost::mutex::scoped_lock lock(m_mutex);
typename std::list<T>::iterator pIter = std::list<T>::begin();
if (pIter == std::list<T>::end())
return false;
out = *pIter;
if (is_pop)
std::list<T>::pop_front();
return true;
}
void clear(bool is_lock = true)
{
if (is_lock)
{
boost::mutex::scoped_lock lock(m_mutex);
std::list<T>::clear();
}else
{
std::list<T>::clear();
}
}
public:
CLockList(void)
{
}
~CLockList(void)
{
clear();
}
};
#endif // __locklist_h__
4 [/b]简单使用例子,假设有一个用户信息类[/b]CUserInfo[/b],基本使用例子如下:[/b][/b]
int main(void)
{
// 定义用户列表
CLockList<CUserInfo> m_users;
// 用户信息实例
CUserInfo user1;
CUserInfo user2;
// 添加
m_users.add(user1);
m_users.add(user2);
// 循环取出
CUserInfo userInfo;
while (m_users.front(userInfo))
{
// ...
}
// 在某些场合需要提前清空列表;
m_users.clear();
return 0;
}
相关文章推荐
- flex4 + spring + blazeds , 使用anonation(注解)机制,利用push技术的实现例子和过程。
- 使用boost::shared_mutex实现读写锁
- SPCAMLEditor使用系列(2)--利用SPCAMLEditor,实现列表顺序号
- boost::asio使用UDP协议通信源码实现
- 使用Python WSGI编写Web服务器前端,并利用Boost.Python在同一进程内连接到C++服务后端实现
- flex4 + spring + blazeds , 使用anonation(注解)机制,利用push技术的实现例子和过程。
- flex4 + spring + blazeds , 使用anonation(注解)机制,利用push技术的实现例子和过程。
- jQuery使用Selectator插件实现多选下拉列表过滤框(附源码下载)
- 合理使用“.NET扩展方法”来简化代码(例子:空值判断,利用扩展方法实现LINQ操作符ForEach)
- iText的使用例子说明(利用Itext画模板导出word、pdf,纯java实现)
- SPCAMLEditor使用系列(2)--利用SPCAMLEditor,实现列表顺序号。
- Android例子源码非第三方实现根据字母排序的城市列表
- Android例子源码非第三方实现根据字母排序的城市列表
- jQuery使用Selectator插件实现多选下拉列表过滤框(附源码下载)
- Android例子源码非第三方实现根据字母排序的城市列表
- 合理使用“.NET 扩展方法”来简化代码(例子:空值判断,利用扩展方法实现LINQ操作符ForEach)
- 第三章 pmtest4源码解析——利用门调用实现特权级转移
- 【原创源码】(03):利用简单递归算法,实现文本匹配、翻译。
- 在python中实现生产者和消费者的例子(三):使用fork和pipe
- 安卓的那些事儿-android之RecyclerView的使用,实现列表横向滚动