feat: automatically detect thread count in parallel finder
This commit is contained in:
parent
ab0613e845
commit
21ca48b9c2
3 changed files with 13 additions and 13 deletions
|
@ -5,23 +5,26 @@
|
|||
|
||||
using std::mutex, std::thread, std::lock_guard, std::vector, std::forward_list;
|
||||
|
||||
ParallelFinder::ParallelFinder(const vector<string> &word_list,
|
||||
size_t thread_count)
|
||||
: word_list_(word_list), thread_count_(thread_count) {}
|
||||
ParallelFinder::ParallelFinder(const vector<string> &word_list)
|
||||
: word_list_(word_list) {}
|
||||
|
||||
forward_list<const string *>
|
||||
ParallelFinder::find_prefix(string_view search_term) const {
|
||||
forward_list<const string *> result;
|
||||
mutex result_mutex;
|
||||
|
||||
const auto word_list_size = word_list_.size();
|
||||
|
||||
const size_t thread_count = thread::hardware_concurrency();
|
||||
|
||||
vector<thread> threads;
|
||||
for (size_t thread_index = 0; thread_index < thread_count_; ++thread_index) {
|
||||
for (size_t thread_index = 0; thread_index < thread_count; ++thread_index) {
|
||||
const size_t first_word_index =
|
||||
thread_index * (word_list_.size() / thread_count_);
|
||||
thread_index * (word_list_size / thread_count);
|
||||
const size_t last_word_index =
|
||||
(thread_index == thread_count_ - 1)
|
||||
? word_list_.size()
|
||||
: (thread_index + 1) * (word_list_.size() / thread_count_);
|
||||
(thread_index == thread_count - 1)
|
||||
? word_list_size
|
||||
: (thread_index + 1) * (word_list_size / thread_count);
|
||||
|
||||
threads.emplace_back(
|
||||
[](const vector<string> &word_list, const string_view &search_term,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue