1

In my code, I want to check whether if a task (in an other thread) is currently running or not.

I was considering using a Dictionary<String, bool>, and checking the boolean value. However, since I don't know all the possible tasks at startup, I will have to populate this Dictionary on the fly when the task is started for the first time and query it like this:

Boolean status;
if (_tasks.TryGetValue(lockName, out  status))
{
    return status;
}
return false;

Since, instead of having this bool, the mere existence of the entry can be informative, I am considering using Hashset<String> and adding/removing values instead of setting the flag.

return _tasks.Contains(name)

I am wondering which of these two approaches would yield the best results. I expect approximately the same number of write operations than reads.

radbyx
  • 9,352
  • 21
  • 84
  • 127
Luk
  • 5,371
  • 4
  • 40
  • 55
  • 1
    I think Guffa is right here but in case it *really* matters you should consider setting up a simple experiment and measure the timing of both - it should not be terrible hard and you get reliable facts this way – Random Dev Feb 24 '12 at 12:08
  • 2
    Agreed, and I'm doing it at the same time. But I thought this would be a good question to ask (for Google search posterity) – Luk Feb 24 '12 at 13:21

1 Answers1

1

There won't be any significant difference.

Both uses a hash lookup to check for the key, which is close to an O(1) operation.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005