使用std :: filesystem和多线程搜索文件

如何解决使用std :: filesystem和多线程搜索文件

我编写了一个程序,使用std :: filesystem在系统上按名称搜索文件(使用std :: filesystem :: recursive_directory_iterator进行递归搜索)。如果我从Linux的根目录(“ /”)开始,则仅使用一个主线程,执行时间约为1秒,即可正常工作。 问题:当我尝试使用多个线程运行该程序时,我异常中止(核心转储),并且文件系统错误:递归目录迭代器无法打开目录:不是目录。我要运行该功能

bool SearchFile::find_file_in_directory(const std::string file_name,fs::path path) 
同时在8个线程中使用

,但是具有不同的参数(路径参数将是另一个)。 我试图通过这种方式实现多线程: 计算系统上所有现有文件数量,然后将该数量除以线程数量,并指定每个线程入口目录以开始搜索。 我也有自己的类,用于处理名为SearchFile(sf)的文件。 下面列出的代码

首先是一个线程的方法(它可以根据需要工作):

bool SearchFile::find_file_from_directory(const std::string file_name,fs::path path) {
  if (fs::is_directory(path))
    for (auto &p : fs::recursive_directory_iterator(
             path,fs::directory_options::skip_permission_denied)) {
      path = p;
      if (path.filename() == file_name) {
        curr_path = path;
        return true;
      } else {
        // uncomment this line to see how the function search file
        // std::cout<<path<<"\n";
        continue;
      }
    }
  else
    std::cout << "Entered path is not a directory\n";

  return false;

  std::cout << "File doesn`t exist\n";
  return false;
}

我使用的SearchFile方法

bool SearchFile::find_file_in_directory(const std::string file_name,fs::path path) {
  if (fs::is_directory(path))
    path /= file_name;
  if (fs::exists(path)) {
    return true;
  } else
    std::cout << "Entered path is not a directory\n";
  return false;

  std::cout << "File doesn`t exist\n";
  return false;
}

u_int SearchFile::get_num_of_files_recursively(const fs::path path) {
  u_int num = 0;
  if (fs::is_directory(path)) {
    for (fs::recursive_directory_iterator it(
             path,fs::directory_options::skip_permission_denied);
         it != fs::recursive_directory_iterator(); ++it) {
      num++;
    }
  } else {
    std::cout << "Entered path is not a directory\n";
    return 0;
  }
  return num;
}

main.cpp

#include <iostream>
#include <string>
#include <thread>
//#include "library/searchfile.h"
#include <condition_variable>
#include <fstream>
#include <vector>
//#pragma once
#include <filesystem>

SearchFile sf;
std::condition_variable cv;
std::vector<fs::path> arr;

void do_division(const fs::path path,const u_int times) {
  u_int files_num = sf.get_num_of_files_recursively(path),part = files_num / times;
  u_int i = 0;
  arr.push_back(path);
  for (fs::recursive_directory_iterator it(path,fs::directory_options::skip_permission_denied);
       it != fs::recursive_directory_iterator(); ++it) {
    if (i == part) {
      arr.push_back(it->path());
      part += part;
    }
    i++;
  }
}

void do_job(const fs::path path,const std::string name) noexcept {
  for (auto &p : fs::recursive_directory_iterator(path,fs::directory_options::skip_permission_denied)) {
    std::cout << std::this_thread::get_id() << " - " << p.path() << "\n";

    if (sf.find_file_in_directory(name,p.path())) {
      cv.notify_all();
      std::fstream f;
      std::string path = p.path().string();
      f.open(path);
      f << path;
      f.close();
      return;
    }
  }
}

int main() {
  std::string filename = "MyFile.txt";
  std::string path = "/";

  do_division("/",8);

  while (1) {
    std::thread t1(do_job,arr[0],filename);
    t1.detach();

    std::thread t2(do_job,arr[1],filename);
    t2.detach();

    std::thread t3(do_job,arr[2],filename);
    t3.detach();

    std::thread t4(do_job,arr[3],filename);
    t4.detach();

    std::thread t5(do_job,arr[4],filename);
    t5.detach();

    std::thread t6(do_job,arr[5],filename);
    t6.detach();

    std::thread t7(do_job,arr[6],filename);
    t7.detach();

    std::thread t8(do_job,arr[7],filename);
    t8.detach();
  }
  return 0;
}

也许这不是解决我的问题的好方法,但我只是一个初学者。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?