I have a strange situation. I am using a variant of BiDictionary<mystruct, int>
implementation here.. I also use the Dictionary type to ConcurrentDictionary
for firstToSecond
The problem is that at the code
ConcurrentDictionary firstToSecond;
public IList<TSecond> GetByFirst(TFirst first)
{
IList<TSecond> list;
var myclone=DeepCopy(firstToSecond);
var res =firstToSecond.TryGetValue(first, out list);
if (!res)
{
return EmptySecondList;
}
var doNextThing = DoNextMethod();
return new List<TSecond>(list); // Create a copy for sanity
}
may sometimes have res=false
. When such a condition occurs, I naturally break at the next statement (var doNextThing = DoNextMethod();
) in my Visual Studio 2015 debugger to examine the value. I put mydict.TryGetValue(item, out index)
in the debugging window and to my puzzlement, now it does return a value ( ie: res=true
)!
Debugging attempt:
I make sure that my key is sane(it is a
struct
, so it should be of an immutable type with an.Equals()
and.GetHashCode()
that do repeatable, predictable things).I also deepcopy
mydict
to another object ( say,myclone
) before I carry out theTryGetValue
operation. I check themyclone.TryGetValue(item, out index)
in the debugger, and it returns true!I suspect this is a multithreading issue, because my code does multithreading. So, I then check whether
myDict
is being modified by other thread in my code, and I find that it is being instantiated and used inside a class, and inside a private method (firstToSecond
is private). It is definitely not a singleton that is accessible from elsewhere, let alone being modified.- I use
ConcurrentDictionary
forfirstToSecond
variable.
- I use
My multithreading debugging skill is very limited, so I don't know how else I can test for multithreading issue. Any ideas how I can tackle the issue?
This is not a dupe of this question! For I am aware that there is a ConcurrentDictionary, but how it can be useful?