处理字符串的最佳线程数

C++ - Optimal number of threads for processing string

本文关键字:线程 最佳 字符串 处理      更新时间:2023-10-16

我有一个长度N的std::string,我想使用线程将长度K的所有子字符串插入到std::set容器中。我应该使用多少std::thread或pthread_t对象?

考虑N = 500,000, K = 3

使用ThreadPool。

它使用起来很简单,你只需要include "ThreadPool.h",你可以根据可用的内核数量设置最大线程数。您的代码应该包含以下代码段:

 int max_threads = std::thread::hardware_concurrency();
 ThreadPool pool(max_threads);
 auto result = pool.enqueue(func,params); 

这里func是要调用的函数,params是参数,返回值将存储在result