I am trying to figure out why the parallel code I have written misses out on certain strings I am trying to search in parallel.
Here is what I have
– StringCollection is a dictionary. With key being a hash and value some string. There are four dictionaries in total which are to be searched.
– InputStrings[] is an array of strings, 10 to be exact which I need to search.
In my code I have
Paralle.Foreach(InputStrings,currentString =>
{
int key = some hashing function on currentString;
if(SearchCollection(key))// boolean result
{
Console.WriteLine("Found:"+key);
}
else
{
Console.WriteLine("Not Found:"+key);
}
});
Once I execute this, I find either 8 or 9 keys, and keep either missing one or two keys. I know the collection contains all the keys I am trying to search.
The key searched is modified inside the function for some further searching on certain conditions. That is key is modified and passed on to some sub dictionary for searching.
So points I raise are;
Does this mean threads are exiting before being completed?
Do I need to put locks over my SearchCollection method or inside my SearchCollection method?
Since I use a single collection for all the threads, does the TPL take care of locking or does it make copies of the collection itself that I am trying to search?
Any help is highly appreciated.
Thanks
Your
SearchCollectionand/or your hashing code appears to be not thread-safe.Usually it is safe to use collection read-only form multiple threads, but it depends on the implementation.
You do not provide enough details to analyze this any further.