• c++ 线程安全的string类


    非安全string

    说明

    c++标准未规定stl容器以及字符串的线程安全性,故std::string在多线程下是不安全的。

    代码示例

    #include 
    #include 
    using namespace std;
    
    std::string *sp = nullptr;
    
    void Read()
    {
    	for(int i = 0; i < 100000; i++)
    	{
    		std::string  s = "";
    		s = *sp;
    	}
    }
    
    void Write()
    {
    	for(int i = 0; i < 100000; i++)
    	{
    		*sp = std::to_string(i);
    	}
    }
    
    int main()
    {
    	while(1)
    	{
    		sp = new std::string("string test");
    		std::thread t1(Read);
    		std::thread t2(Write);
    		t1.join();
    		t2.join();
    		delete sp;
    		sp = nullptr;
    	}
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    安全string

    安全string类封装

    • 使用std::recursive_mutex递归锁保证string多线程下的读写安全;

    关于std::recursive_mutex,可以查看:https://blog.csdn.net/www_dong/article/details/132197541

    class TSString 
    {
    	typedef std::lock_guard RecursiveGuard;
    	mutable std::recursive_mutex rmutex;   // 注意增加mutable修饰
    	std::string str_;
    
    public:
    	TSString() {}
    
    	TSString(const TSString& str)
    	{
    		Set(str.Get());
    	}
    
    	TSString& operator= (const TSString& str)
    	{
    		if(this == &str)
    			return *this;
    
    		Set(str.Get());
    		return *this;
    	}
    
    	void Set(const char* str)
    	{
    		RecursiveGuard mtx(rmutex);
    		if(NULL == str)
    		{
    			str_ = "";
    		}
    		else
    		{
    			str_ = str;
    		}
    	}
    
    	void Set(const std::string& str)
    	{
    		RecursiveGuard mtx(rmutex);
    		str_ = str;
    	}
    
    	std::string Get() const
    	{
    		std::string str;
    		{	
    			RecursiveGuard mtx(rmutex);
    			str = str_;
    		}
    
    		return str;
    	}
    
    	int Compare(const char* str) const 
    	{
    		if(NULL == str)
    			return 1;
    
    		RecursiveGuard mtx(rmutex);
    		return str_.compare(str);
    	}
    
    	int Compare(const std::string& str) const
    	{
    		return Compare(str.c_str());
    	}
    
    	int Compare(const TSString& s) const
    	{
    		return Compare(s.Get());
    	}	
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    代码示例

    TSString *sp = nullptr;
    
    void Read()
    {
    	for(int i = 0; i < 5000; i++)
    	{
    		TSString s;
    		s.Set("");
    		s = *sp;
    	}
    }
    
    void Write()
    {
    	for(int i = 0; i < 5000; i++)
    	{
    		sp->Set(std::to_string(i));
    	}
    }
    
    int main()
    {
    	while(1)
    	{
    		TSString s;
    		s.Set("string test");
    		sp = new TSString(s);
    		std::thread t1(Read);
    		std::thread t2(Write);
    		t1.join();
    		t2.join();
    		delete sp;
    		sp = nullptr;
    	}
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
  • 相关阅读:
    上市公司员工及工资数据(2000-2022年)
    Python学习笔记-数字类型
    C++ 11 智能指针
    pandas reindex 方法
    Python 基于 OpenCV 视觉图像处理实战 之 OpenCV 简单人脸检测/识别实战案例 之二 简单人脸检测添加戴眼镜效果
    剑指offer 76 删除链表中重复的点
    【BOOST C++ 5 】通信(01 Boot.Asio )
    计算机网络 5 - 链路层
    机器学习之查准率、查全率与F1
    在Python上用openSMILE提取IS09和eGeMAPS特征集
  • 原文地址:https://blog.csdn.net/www_dong/article/details/134099530