Skip to content

Instantly share code, notes, and snippets.

@kebby
Created November 7, 2014 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kebby/7040ceeb42fb8f79eb65 to your computer and use it in GitHub Desktop.
Save kebby/7040ceeb42fb8f79eb65 to your computer and use it in GitHub Desktop.
// stuff you should know:
abstract class OutgoingCallBase
{
// ...
}
private readonly ConcurrentDictionary<int, OutgoingCallBase> OutgoingCalls = new ConcurrentDictionary<int, OutgoingCallBase>();
private int CallId;
// so I have this piece of async code that gets called now and then...
[...]
int id = Interlocked.Increment(ref CallId);
var call = new OutgoingCall<T> { /* ... */ };
OutgoingCalls.TryAdd(id, call);
[...]
// ... and another piece of code that's run in regular intervals in another Task
[...]
var calls = OutgoingCalls.ToList();
if (calls.Count == 0)
break;
[...]
/*
Now what happens is that sometimes, "calls" contains an entry { 0, null } - which I NEVER add into the dictionary, EVER. You
wouldn't find it if you looked for it in the OutgoingCalls dict itself, and it also doesn't show up in the debugger there.
ConcurrentDictionary.GetEnumerator is promised to be thread safe ( http://msdn.microsoft.com/en-us/library/dd287131%28v=vs.110%29.aspx )
They say you could get entries mid-modification but I would assume this doesn't mean not-really-constructed temp objects, thank you very much
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment