C++多线程读写文件

std::mutex mt_;

std::string LoadFile(const std::string &path) {
    std::unique_lock<std::mutex> lck(mt_);
    std::ifstream fin(path);
    std::string str((std::istreambuf_iterator<char>(fin)),
                    std::istreambuf_iterator<char>());
    return str;
}

int WriteFile(const std::string &path, const std::string &content) {
    std::unique_lock<std::mutex> lck(mt_);
    int ret = -1;
    do {
        std::ofstream out;
        out.open(path, std::ios::trunc);
        if (!out.is_open())
            break;

        out << content;
        out.close();

        ret = 0;
    } while (0);

    return ret;
}

相关推荐

  1. C++线文件

    2024-07-12 16:40:03       25 阅读
  2. longjmp和线线实例

    2024-07-12 16:40:03       21 阅读
  3. JVM线和锁

    2024-07-12 16:40:03       60 阅读
  4. C# 不用lock一个线程序

    2024-07-12 16:40:03       31 阅读
  5. c++的文件

    2024-07-12 16:40:03       56 阅读
  6. C++的文件

    2024-07-12 16:40:03       67 阅读
  7. C++BMP文件

    2024-07-12 16:40:03       34 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 16:40:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 16:40:03       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 16:40:03       58 阅读
  4. Python语言-面向对象

    2024-07-12 16:40:03       69 阅读

热门阅读

  1. 利用大数据分析股市异常资金

    2024-07-12 16:40:03       21 阅读
  2. PyTorch 2-深度学习-模块

    2024-07-12 16:40:03       22 阅读
  3. 科锐国际2024年OD招聘,技术岗急招

    2024-07-12 16:40:03       22 阅读
  4. 使用 CSS 实现渐变效果

    2024-07-12 16:40:03       26 阅读
  5. 近期计算机领域的热点技术

    2024-07-12 16:40:03       24 阅读
  6. Spring Boot中如何集成ElasticSearch进行全文搜索

    2024-07-12 16:40:03       24 阅读
  7. C++ 识别 .lua文件内用户自定义的全局函数

    2024-07-12 16:40:03       23 阅读
  8. 临终护理:用爱与关怀陪伴生命的最后旅程

    2024-07-12 16:40:03       22 阅读