Locking based on parameters

Viewed 3382

Suppose I have this method:

void Foo(int bar)
{
    // do stuff
}

Here is the behavior I want Foo to have:

  1. If thread 1 calls Foo(1) and thread 2 calls Foo(2), both threads can run concurrently.

  2. If thread 1 calls Foo(1) and thread 2 calls Foo(1), both threads cannot run concurrently.

Is there a good, standard way in .net to specify this type of behavior? I have a solution that uses a dictionary of objects to lock on, but that feels kind of messy.

4 Answers
Related