Saturday, 31 August 2013

C# I need a algorithmic for Lock-by-value

C# I need a algorithmic for Lock-by-value

You have a function Foo(long x) which is called by many threads. You want
to prevent threads from executing the method simultaneously for the same
value of x. If two methods call it with x=10, one should block and wait
for the other to finish; but if one thread calls it with x=10 and another
with x=11, they should run in parallel. X can be any long value. You must
not keep in memory all the values with which the function was called in
the past, but free them once all threads with that value exit the function
(you may wait for GC to free them). Extra: if two threads, with different
values of X, do not contend for any shared lock (such as a static
management lock) when entering or leaving the function, and only threads
with the same values of X can ever block and wait.

No comments:

Post a Comment