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

线程中future/atomic/async及nlohmann json的学习

1)std::future的其他成员函数

wait_for()成员函数,wait_for返回的是一个std::future_status枚举类型,根据返回值做相应处理
std::future_status state = result.wait_for(std::chrono::seconds(1));
if(state == std::future_status::ready)
{
cout<<result.get()<<endl;
cout<<“ready”<<endl;
}

2)std::future的类模板中的get()方法,采用的是移动语义,所有不能多次get();

  shared_future也是一个类模板,get()成员函数是复制操作,所以可以多次get()#include <iostream>
#include<thread>
#include<mutex>
#include<list>
#include<future>using namespace std;int func(void)
{cout<<"start----- thread_id is : "<<std::this_thread::get_id()<<endl;std::this_thread::sleep_for(std::chrono::milliseconds(2000));cout<<"end-------- thread_id is : "<<std::this_thread::get_id()<<endl;return 5;
}int main()
{cout<<"main thread start----- thread_id is : "<<std::this_thread::get_id()<<endl;std::future<int> result = std::async(func); std::shared_future<int> sharedRes = result.share(); //用future的shared成员函数返回值,生成一个shared_future对象cout<<sharedRes.get()<<endl;  //shared_future可以多次getcout<<sharedRes.get()<<endl;  cout<<"********main run*********"<<endl;   return 0;
}

3)原子操作:std::atomic的使用,atomic是一个类模板

//可以用互斥锁进行共享数据访问,但用原子操作的话,用的时间比互斥锁使用的时间短;
//原子操作主要操作的是一个变量,一般用于计数或者统计!
#include <iostream>
#include<thread>
#include<atomic>using namespace std;std::atomic<long> gCount; //封装成一个原子类对象;定义成一个原子的全局量;
void func(void)
{for(long int i = 0; i<10000000; i++){gCount++; //gCount = gCount + 1;这样写,数据就不对了,原子操作不是所有的操作符都成立!!!//当拿不准的时候,就得需要测试一下;}
}int main()
{gCount = 0;thread t1(func);thread t2(func);t1.join();t2.join();cout<<gCount<<endl;return 0;
}

4)std::async深入学习

async(),我们不叫他创建一个线程(虽然有时候可以创建一个线程),我们称创建一个异步任务!
async和thread最大的区别就是,async有时候不会创建线程。
std::future::deferred和std::launch::async是绑定在一起的,叫延迟调用!
用std::future_status就可以判断是延迟调用还是创建异步任务(创建新线程)#include <iostream>
#include<thread>
#include<future>using namespace std;int func(void)
{cout<<"start----- thread_id is : "<<std::this_thread::get_id()<<endl;std::this_thread::sleep_for(std::chrono::seconds(2));return 1;
}int main()
{cout<<"main----- thread_id is : "<<std::this_thread::get_id()<<endl;// 如果使用了std::launch::deferred这个枚举量,则func会延迟到future里面的get()或者wait()才会执行!!!// 如果一直没有遇到future的get或者wait函数,则func根本不会执行;// std::launch::async,强制这个异步任务在新线程执行;std::future<int> result = std::async(std::launch::deferred|std::launch::async,func); //缺省值为: std::launch::async|std::launch::deferred,由系统自行决定!// auto num = result.get(); //如果屏蔽到此条语句,func()函数根本不会执行!// cout<<num<<endl;result.wait();return 0;
}//自动析构技术lock_guard<std::mutex> lock(mtx);
class A{ };// 仿写lock_guard功能
class LockGuard
{
public:LockGuard(A* ptr) //进入构造函数加锁{a_ = ptr;mtx.lock();}~LockGuard() //析构函数执行解锁{mtx.unlock();}
private:A* a_;        std::mutex mtx;
};

两个中文名:
//std::mutex 独占互斥量
//std::recursive_mutex 递归独占互斥量

5)nlohmann::json的学习
使用方法:
5.1)直接包含“single_include/nlohmann/json.hpp”头文件,这个头文件从网上一找一大片,很容易,包含完就可以用了。
5.2)理解并掌握一些成员函数,依然从网上找,so easy!!!

#include <iostream>
#include<fstream>
#include "single_include/nlohmann/json.hpp"using json = nlohmann::json;
using namespace std;void writeFile(json& obj)
{obj.push_back({"1","jianghuaiwei"});obj.push_back({"2","suhui"});        std::ofstream("/home/jiang/11.txt") <<obj; //写文件
}void readFile(json& obj)
{for(auto &e : obj.items()){cout<<e.key()<<" "<<e.value()<<endl;}
}int main()
{json jj;// writeFile(jj); //写文件std::ifstream("/home/jiang/11.txt") >> jj; //读文件readFile(jj);return 0;
}
http://www.lryc.cn/news/160642.html

相关文章:

  • windows安装MongoDB后进入命令交互界面失败解决方案
  • 基于Java+SpringBoot+Vue前后端分离高校专业实习管理系统设计和实现
  • E. Hanging Hearts
  • docker安装RabbitMQ教程
  • Java虚拟机整型数加载指令学习
  • Docker 实现 MySQL 一主一从配置
  • Python编程练习与解答 练习113:避免重复
  • 线上 udp 客户端请求服务端客户端句柄泄漏问题
  • 合宙Air724UG LuatOS-Air LVGL API控件-窗口 (Window)
  • 80 # 图片防盗链
  • App自动化测试持续集成效率提高50%
  • LeetCode —— 复写零(双指针)
  • 【Vue篇】Vue 项目下载、介绍(详细版)
  • Python批处理(一)提取txt中数据存入excel
  • 只考一门数据结构!安徽工程大学计算机考研
  • Ubuntu 20.04出现蓝牙无法打开的问题(已解决)
  • 并发测试工具 apache-jmeter使用发送post请求JSON数据
  • 牛客练习赛115 A Mountain sequence
  • 通过git bash激活虚拟环境遇到的问题
  • EasyAVFilter代码示例之将摄像机RTSP流转成RTMP推流输出
  • 【【C语言康复训练-4】】
  • [DM8] DM-DM DBLINK DPI方式
  • 创建了一个名为nums_list的vector容器,其中存储了一系列的pair<int, int>
  • SpringMVC文件上传、文件下载多文件上传及jrebel的使用与配置
  • Leetcode143. 重排链表
  • Git 回顾小结
  • 响应式布局(3种) + flex计算
  • Pytorch从零开始实战01
  • inappropriate address 127.0.0.1 for the fudge command, line ignored 时间同步的时候报错
  • linux并发服务器 —— 项目实战(九)