From eda69b1418531666db932f278055807a982cb7e2 Mon Sep 17 00:00:00 2001 From: cflip Date: Sun, 11 Dec 2022 12:17:31 -0700 Subject: Implement multithreading --- hashstrings.cpp | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/hashstrings.cpp b/hashstrings.cpp index 35d9dac..2ec1544 100644 --- a/hashstrings.cpp +++ b/hashstrings.cpp @@ -1,12 +1,21 @@ +#include #include #include +#include #include #include +#include +#include + +#define MAX_GENERATED_STRING_LENGTH 8 // Toggles whether generated strings are printed as they are generated. It looks // cool and lets you see the progress, but slows down the process a lot. #define PRINT_GENERATED_STRINGS 0 -#define MAX_GENERATED_STRING_LENGTH 8 + +// This is the minimum length for generating strings where we start computing +// across separate threads instead of in parallel. +#define MULTITHREADING_THRESHOLD 6 // This function uses the same algorithm as Java's String.hashCode method. This // implementation was stolen from OpenJDK. @@ -74,10 +83,22 @@ bool generate_all_strings(const std::string& prefix, int hash_to_find, int k) return compare_with_case_insensivity(string_to_test, hash_to_find); } - for (char c = 'a'; c <= 'z'; c++) { - std::string new_prefix; - new_prefix = prefix + c; - found |= generate_all_strings(new_prefix, hash_to_find, k - 1); + if (k >= MULTITHREADING_THRESHOLD) { + // Create a vector containing the alphabet so I can use std::for_each + std::vector alphabet; + for (char c = 'a'; c <= 'z'; c++) { + alphabet.push_back(c); + } + + std::for_each(std::execution::par, alphabet.begin(), alphabet.end(), [&](char c) { + std::string new_prefix = prefix + c; + found |= generate_all_strings(new_prefix, hash_to_find, k - 1); + }); + } else { + for (char c = 'a'; c <= 'z'; c++) { + std::string new_prefix = prefix + c; + found |= generate_all_strings(new_prefix, hash_to_find, k - 1); + } } return found; -- cgit v1.2.3