Depending on how your dictionary looks, you might not need locks at all if you can get each thread to create independent subtrees. If this is not an online algorithm, precede the words with a prefix (the first letter, if you have <26 threads, the first and second, if you have more or you know that the data is unbalanced, for example, 90% of the word starts with A). Basically, it will be an O (n) operation, where you make one pass to count the number of words that begin with a given letter, then one pass to sort (along the sort lines of your choice). Then split the prefixes between the threads and create each thread of these independent sub-ribs. Finally, one thread adds each of these subtrees to the root. I will review an example below.
Your dictionary:
Bark
Apple
Cookie
AND
Baby
Corn
Blue
cake
Bacon
After sorting:
Apple
AND
Bark
Baby
Blue
Bacon
Corn
Cookie
Cake
Then we divide the prefixes among the threads. For this example, we have 3 threads that receive the prefixes [A] [B] [C] and build the following trees:
A - | B ------- | C ------- |
PN | - A --- | LO --- | A
PDRBCUORK
LKYOEKNE
ENI
E
And then you have one thread combining them in the root sequence:
| ----------- Root ------------------ |
A - | B ------- | C ------- |
PN | - A --- | LO --- | A
PDRBCUORK
LKYOEKNE
ENI
E
I hope this made sense.
Advantages of this method: Streams work almost independently of each other, and you do not have the overhead associated with the acquisition and release of locks.
The disadvantages of this method: If you do not know anything about the dictionary, a serious workload imbalance can occur, and in the worst case (say, all words begin with βAβ), it returns to the fact that it is basically the thread that creates the tree. There are several ways to do it better, for example, you can add some checks when sorting, so that if the workload is very unbalanced when working with one letter prefix, use the first two letters, but in fact you canβt guarantee that it will balanced.
You can also have time streams, if you say that you have 20 threads and sorted by the first letter, then you will have 6 threads that must make two subtrees, and 14 of them sit idle for half the time. You can subdivide the subtrees again to handle this, but this is the extra time spent on the preprocessing step.
In any case, there is no guarantee that this is faster than your method, but consider something.