I am using XxHash in my app and was assuming that – for the very same exact input – the resulting value would always be the same.
I have spotted however some irregular behavior when running in multithreaded contexts.
I created the excerpt below that reproduces the problem:
void reproduce()
{
var scrubber = new MyScrambler("initialSeed");
var input = "MyString";
var tasks = new List<Task>();
var scrubbedValuesList = new ConcurrentBag<string>();
for (var i = 0; i < 100; i++)
{
tasks.Add(Task.Run(() =>
{
var scrubbedValue = scrubber.Scramble(input);
scrubbedValuesList.Add(scrubbedValue);
}));
}
Task.WaitAll(tasks.ToArray());
Console.WriteLine($"distinct hashed values for input: [{input}]: nn ===> {string.Join("n ===> ", scrubbedValuesList.Distinct())}");
}
Running it often gives me multiple results, like
Seldom, I get a distinct result only.
Is this expected behavior ? What is it with the multithreaded context that makes it non deterministic ?