I want to multi-thread a "brute-force" algorithm.
I have a function bool check(const char *s, size_t n)
that checks if the string s
of size n
is the password or not.
This function is called for all string combinations that are possible with my alphabet (about 90 chars in the alphabet).
I was able to do it in one thread, but now I want to multi-thread it. I also want my threads to each have almost the same amount of work I do not want one thread to have to test 1B combinations and the other one only 1k).
I thought of splitting the load in "batches" of combinations. Each batch would have a start
index and a stop
index so the thread having the batch has to test all the combinations between start
and stop
.
What I call the index of a combination is its lexicographical index, for example with alphabet [A, B, C]:
index combination
1 A
2 B
3 C
4 AA
5 AB
6 AC
7 BA
8 BB
... ...
For example, a thread who would be assigned the batch with start=4
and stop=7
would test combinations AA, AB, AC, BA
.
How can I easily generate the combinations for a batch in a thread? The priority is for it to be fast, since the whole point of the program is brute forcing.
Is there another fast way to split the work between threads?