Use of std::future vs. std::thread in grep clone

I wrote a grep clone recently. It does a recursive search of a string query through all files of a specified directory. The core of program is the function shown below.

/** Do recursive search of query string in files under the given directory.  */
void Search::searchFiles()
{
    std::vector<std::future<void>> futures;
    auto files = fs::recursive_directory_iterator(_directory);
    for (auto& file : files) {
        if (!fs::is_directory(file)) {
            futures.emplace_back(std::async(&Search::searchFile, this, file));
        }
    }
    for (auto & ftr : futures) ftr.wait();
}

I create many futures, but I could have used std::thread. For instance, in the code snippet below searchFiles uses std::thread instead.

void Search::searchFiles()
{
    std::vector<std::thread> threads;
    auto files = fs::recursive_directory_iterator(_directory);
    for (auto& file : files) {
        if (!fs::is_directory(file)) {
            threads.emplace_back(&Search::searchFile, this, file);
        }
    }
    for (auto & th : threads) th.join();
}

Is one or the other implementation better? Performance is one aspect of my question. I am interesting to learn about trade-offs between task-based and thread-based parallelism.

Additionally, how might one implement searchFiles with iterators? The idea being a fixed number of threads would be created, and each searchFile instance would increment the file iterator if there were more files to get. Below is sketch of an idea I had.

void Search::searchFile(FileIterator it)
{
    while(true) {

        // get next file from iterator
        std::unique_lock<std::mutex> ulock(_mutex);
        if (it == it.end()) break;
        auto file = *it;
        ++it; 
        ulock.unlock();
        
        // open file and loop through lines.
        std::ifstream filestream(file);
        ...
}

void Search::searchFiles()
{
    std::vector<std::thread> threads;
    auto fileIterator = fs::recursive_directory_iterator(_directory);

    // create only 10 threads
    for (int idx = 0; idx < 10; idx++) {
        if (!fs::is_directory(file)) {
            threads.emplace_back(&Search::searchFile, this, fileIterator);
        }
    }
    for (auto & th : threads) th.join();
}

KPI

Is one or the other implementation better?

You didn’t tell us your key performance indicator, your metric for “better”,
leaving both of them equally good absent comparison criteria.

Suppose that on a 16-core machine we take “elapsed time” as such a criterion.
Then this submission would be about performance,
yet it includes no performance measurements.
The OP can record such timings on his target host of interest; we cannot.

stragglers

Some files are short, while some are long.
This information is available up front via the POSIX stat() call.
Longer files will tend to imply longer elapsed times during a benchmark.
The OP code schedules files in arbitrary order,
with a decent chance of a long file appearing near the end.
We call such a matching task a “straggler”,
as it will keep a core busy after many other cores have gone idle.

On an N-core machine this code should immediately schedule N-1 tasks,
then identify all the input file sizes and sort on that.
Then execute the remaining tasks in descending size order.
That guarantees that toward the end of a run we will be assigning
short duration tasks, so all cores will tend to go idle at around the same time.

1

Use a thread pool and a thread-safe queue

As J_H already mentioned, you should start by creating a thread pool with around N-1 new threads on a machine with N cores. The main thread can then scan the directories, and add files to a thread-safe queue. The other threads can pick items from the queue to work on.

Adding iterators to the queue might be unsafe, unless you can guarantee that the iterators will remain valid until all files have been processed. Instead you could consider storing filenames in the queue.

No need to explicitly wait for futures

The destructor of a std::future will already call wait() for you, so you don’t have to do this explicitly at the end of searchFiles().

Note that if you would use std::jthread instead of std::thread, you also don’t need to explicitly join() anymore.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật