Learning of future/atomic/async and nlohmann json in threads

1) Other member functions of std::future

wait_for() member function, wait_for returns a std::future_status enumeration type, and performs corresponding processing according to the return value
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) The get() method in the class template of std::future uses move semantics, so it cannot get() multiple times;

  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可以多次get
        cout<<sharedRes.get()<<endl;  
        cout<<"********main run*********"<<endl;   
        return 0;
}

3) Atomic operations: the use of std::atomic, atomic is a class template

//可以用互斥锁进行共享数据访问,但用原子操作的话,用的时间比互斥锁使用的时间短;
//原子操作主要操作的是一个变量,一般用于计数或者统计!
#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 in-depth learning

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;
};

Two Chinese names:
//std::mutex exclusive mutex
//std::recursive_mutex recursive exclusive mutex


5) Learning and usage of nlohmann::json
: 5.1) Directly include the "single_include/nlohmann/json.hpp" header file. This header file can be found in large quantities on the Internet. It is very easy. Once included, it can be used.
5.2) Understand and master some member functions, still find them online, 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;
}


Guess you like

Origin blog.csdn.net/qq_30143193/article/details/132754769