C++11 下的线程安全模板对象
来源:互联网 发布:c4d最新版软件下载 编辑:程序博客网 时间:2024/06/16 13:24
C++11标准下我们可以更加简单地调用线程,但是线程调用就必须注意对象线程安全的问题。
我们可以用智能指针简单地去解决对象线程安全问题,我尝试了解什么叫智能指针,但是最后由于脑子不够用压根不懂他在说什么鬼,所以我尝试自己写个线程安全的容器。
这个线程容器使用的是C++11的一些新特性去实现的,所以不要尝试用VC6神器去编译。
首先需要用到模板类而不是继承,因为使用模板才能确保我不用再去为这件破事而烦恼。
CSafeMap.hpp
#include <functional>
#include <mutex>
#include <map>
using namespace std;
typedef enum {
em不存在该对象 = 0,
em对象使用中 = 1,
em对象空闲 = 2,
em调用对象成功 = 2
}enCSafeMapStatus;//定义个枚举状态
template<class T1>
class CSafeObject
{
public:
CSafeObject() { m_Mark = em不存在该对象; }
CSafeObject(const CSafeObject& cc) { *this = cc; }
virtual ~CSafeObject(void) { }
public:
bool CreateObject(std::function<bool(T1&)> Func)
{
std::lock_guard<std::mutex> hold(m_mtx);
if(m_Mark != em不存在该对象)//避免重复创建对象
return true;
bool bret = Func(m_data );
if(bret )
m_Mark = em对象空闲;
return bret ;
}
void CtrlItem(std::function<void(T1&)> Func)
{
bool bret = false;
do
{
{
std::lock_guard<std::mutex> hold(m_mtx);
if (m_Mark == em不存在该对象)return;//因为对象随时会被释放
if (m_Mark != em对象空闲) continue;//等候对象空闲,如果代码写得足够坑爹就会在这里死锁,测试的时候可以引入个计时器来退出,而release时就不要了。
m_Mark = em对象使用中;//修改状态计数
}
Func(m_data); //调用对象
{
std::lock_guard<std::mutex> hold(m_mtx);
m_Mark = em对象空闲;//修改状态计数
}
bret = true;
} while (bret == false);
}
void Delete(std::function<void(T1&)> Func = [](T1&itr) {if (itr)delete itr; itr = nullptr; })
{
bool bret = false;
do
{
{
std::lock_guard<std::mutex> hold(m_mtx);
if (m_Mark == em不存在该对象)return;
if (m_Mark != em对象空闲) continue;
m_Mark = em对象使用中;
}
Func(m_data);
{
std::lock_guard<std::mutex> hold(m_mtx);
m_Mark = em不存在该对象;//修改状态计数-宣布死亡
}
bret = true;
} while (bret == false);
}
private:
T1 m_data;//需要在多个线程中使用到的对象
int m_Mark;//状态计数器
std::mutex m_mtx;//锁
};
....
例子:
int main()
{
CSafeObject<CString> strData;
strData.CreateObject([](CString&tm)->bool {
tm = _T("");
return true;
});
thread th1([&]() {
do
{
strData.CtrlItem([](CString&tm) {
tm+=_T("Fu*k thread !");
});
this_thread::sleep_for(chrono::microseconds(1));
} while (1);
});
thread th2([&]() {
do
{
strData.CtrlItem([](CString&tm) {
tm.Empty();
});
this_thread::sleep_for(chrono::microseconds(2));
} while (1);
});
thread th3([&]() {
do
{
strData.CtrlItem([](CString&tm) {
tm+=_T("I love you!");
});
this_thread::sleep_for(chrono::microseconds(3));
} while (1);
});
thread th4([&]() {
do
{
strData.CtrlItem([](CString&tm) {
NowLogStringEx(_T("say:%s"), tm);
});
this_thread::sleep_for(chrono::microseconds(4));
} while (1);
});
th1.detach();
th2.detach();
th3.detach();
th4.detach();
......
strData.Delete([](CString&itr){});//释放对象,因为不是new出来的所以在这里载入一个函数释放对象
}
...
这个容器很贱单,就是锁上对象然后操作对象,这里我使用了模板类,std::function,std::mutex,std::lock_guard和Lambel表达式去实现这个线程安全的模板。如果我们使用多个对象时这个类的简便性就越明显,毕竟不在需要去担心锁有没有释放,那一把锁负责那个变量的问题,不过需要注意的是不要嵌套使用CtrlItem,需要调用到多个对象的时候应使用拷贝对象的方式即:
CString str1;
strData_1.CtrlItem([](CString&tm) {str1 =tm;});
strData_2.CtrlItem([](CString&tm) { tm = str1;});
完整源码下载:https://github.com/Reloner/CSafeObject
【完】
- C++11 下的线程安全模板对象
- linux下C++线程安全对象之互斥锁的应用
- 线程安全的对象操作
- 线程安全的对象池
- 一些线程安全的对象
- C#线程安全对象的内部实现
- 对象耦合的非线程安全
- C++线程安全的对象生命周期管理
- java中线程安全的集合对象
- python 线程安全的对象池
- 线程安全的对象生命期管理
- 线程安全的对象生命周期管理
- c下的线程
- FILE对象线程安全
- c# SingleTon的线程安全(存档)
- Linux下的strerror是否线程安全?
- 多线程下线程安全的单列模式
- Concurrent下的线程安全集合淄滋爪篆装状
- c++ 学习之路 第五天 const理解
- DELPHI中的快捷方式一览(完全正式版)
- C语言11
- Volley框架
- 金融、支付行业的开发者不得不知道的float、double计算误差问题
- C++11 下的线程安全模板对象
- Delphi使用技巧四则
- poj-2240-Arbitrage(Bellman-ford算法练习 + Floyd算法练习)
- 手摇算法(三重反转算法/内存反转算法)
- Tickets 1260
- Libcurl多线程crash问题
- 新博客:http://tedhacker.top/
- 杭电-1260 Tickets
- redis的五种数据结构及使用场景