首页 > 编程语言 >c++14 读写锁

c++14 读写锁

时间:2023-03-28 15:34:28浏览次数:31  
标签:std 14 读写 readJsonFile c++ static file otherSettingModel jsonFileSavePath

读的时候用共享锁,写的时候用独占锁

struct  otherSettingModel
{
    inline static const char*  jsonFileSavePath = "../data/otherSettingModel.json";
    inline static std::shared_timed_mutex fileLocker{};
    static otherSettingModel  fromFile()
    {
        std::shared_lock<std::shared_timed_mutex> readLocker(fileLocker);
        Q_UNUSED(readLocker)
        if(QFile::exists(jsonFileSavePath))
        {
            QString readJsonFile;
            QFile file(jsonFileSavePath);
            if (file.open(QIODevice::ReadWrite))
            {
                readJsonFile = file.readAll();
                file.close();
            }
            otherSettingModel result =  Prism::Json::fromJsonString<otherSettingModel>(std::move(readJsonFile));
            return result;

        }
        else
        {
            saveToFile(otherSettingModel());
            return fromFile();
        }
    }
    static void saveToFile(otherSettingModel&& model)
    {
        std::lock_guard<std::shared_timed_mutex> writeLocker(fileLocker);
        Q_UNUSED(writeLocker)
        QFile file(jsonFileSavePath);
        file.remove();
        if (file.open(QIODevice::ReadWrite))
        {
            QTextStream stream(&file);
            stream.setCodec("UTF-8");
            stream << Prism::Json::toJsonString(model,true);
        }

    }

}

标签:std,14,读写,readJsonFile,c++,static,file,otherSettingModel,jsonFileSavePath
From: https://www.cnblogs.com/nocanstillbb/p/17265359.html

相关文章

  • 14:SwiftUI-Stepper
      正文 ////StepperPage.swift//SwiftUIDeom////Createdbyzhoukang03on2023/3/28.//importSwiftUIstructStepperPage:View{@Stateva......
  • C++智能指针、绑定器和函数对象、lambda表达式
    智能指针​ 智能指针可以保证资源的自动释放不带引用计数的智能指针auto_ptr只让最后一个指向的指针管理资源,之前的auto_ptr会被置为nullptrscoped_ptr删除了拷贝构造......
  • Walkthrough-KIOPTRIX 2014
    0x01环境靶机地址:https://www.vulnhub.com/entry/kioptrix-2014-5,62/靶机默认网卡有点问题,移除网卡再新增网卡即可环境容易崩溃,崩溃了重启就好0x02过程1.信息收集......
  • C++开发方向书籍推荐
    如果你正在学习C++,那么一本好的教材或参考书可以事半功倍。以下是几本我个人推荐的C++书籍或视频:C++基础看书C++PrimerC++程序设计语言EffectiveC++MoreEffectiv......
  • AtCoder Beginner Contest 145
    AtCoderBeginnerContest145https://atcoder.jp/contests/abc145D-Knight乍一看以为是dp,但是数据范围不允许。仔细一看发现,两种操作的次数是固定的,可以枚举出来每......
  • AtCoder Beginner Contest 148
    AtCoderBeginnerContest148https://atcoder.jp/contests/abc148这场比较简单D-BrickBreak二分orLIS#include<bits/stdc++.h>#definelllonglongusingn......
  • IMU和GPS ekf融合定位 从matlab到c++代码实现 基于位姿状态方程,松耦合
    IMU和GPS ekf融合定位从matlab到c++代码实现基于位姿状态方程,松耦合文档原创且详细YID:6745659043907933......
  • Office2010安装错误1402问题
    http://blog.sina.com.cn/s/blog_555ea2470101831d.htmlsecedit/configure/cfg%windir%\inf\defltbase.inf/dbdefltbase.sdb/verbose......
  • C++11之智能指针shared_ptr
    在C++开发中,我们经常会遇到程序运行中突然崩溃、程序运行所用内存越来越多最终不得不重启等问题,这些问题往往都是内存资源管理不当造成的。C++11新标准中,增添了uni......
  • CS143——第一章
    课程地址:Youtu视频:StanfordCS143CompilersIntrotoCompilers编译器和解释器编译器:offline离线输入:程序输出:exec过程:在对输入数据进行处理前不会对程序进行处理......