当前位置: 首页 > news >正文

自媒体运营从入门到精通长春seo排名公司

自媒体运营从入门到精通,长春seo排名公司,郑州网站建设套餐,百度提交非安全string 说明 c标准未规定stl容器以及字符串的线程安全性&#xff0c;故std::string在多线程下是不安全的。 代码示例 #include <iostream> #include <stdio.h> using namespace std;std::string *sp nullptr;void Read() {for(int i 0; i < 100000;…

非安全string

说明

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

代码示例

#include <iostream>
#include <stdio.h>
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;
}

安全string

安全string类封装

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

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

class TSString 
{typedef std::lock_guard<std::recursive_mutex> 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());}	
};

代码示例

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;
}
http://www.hkea.cn/news/379815/

相关文章:

  • 天津手机网站建站培训代运营公司可靠吗
  • 网站制作的一般步骤长春网站优化平台
  • Python做网站 性能上海seo培训中心
  • 网上投诉平台公众号排名优化
  • 网页模板网站推荐媒体公关是做什么的
  • 泰安的网站建设公司爱站网域名查询
  • 台州椒江网站制作公司广告推销
  • 南康做网站合肥seo招聘
  • 成都网站建设定长沙专业网站制作
  • 有什么网站是python做的如何自己开发一个平台
  • 网站建设标志设计北京网站优化公司
  • 图标使用wordpress杭州seo博客
  • 企业网站如何做推广竞价推广托管公司介绍
  • 网站如何做微信登录seo公司 杭州
  • 中山里水网站建设软文广告案例分析
  • 做外贸是用什么网站做新型网络营销方式
  • 心理咨询网站开发百度手机seo软件
  • 17网站一起做网批seo营销优化
  • 做赚钱网站程序员培训班要多少钱
  • 已经收录大规模修改收录页面对网站有影响吗什么软件可以推广自己的产品
  • 丁香园做科室网站厦门网络推广
  • 免费的企业网站制作提高网站权重的方法
  • 兰州网站制作怎么样网页在线生成
  • 自建网站网址雅虎搜索引擎首页
  • 注册科技有限公司可以做网站吗百度搜索排名机制
  • 武汉做网站好网站制作多少钱一个
  • 安阳网站建设怎么从网上找客户
  • 文章博客媒体网站模板怎样在百度上打广告
  • 做网站是不是要模板直接打开百度
  • 哪个网站做app推广服务商