Suppose I need to read/write 1000 files from multiple threads (i.e. one file could be written from multiple threads). I don't want to have one lock to protect all files, since that would be slow. All I can think of is to have a List<object> to hold 1000 locks and do something like lock(list[i]) { write to i'th file}. Is that the right way to do that?
This would be the approach:
static object _list_lock = new object();
static List<object> locks = new List<object>();
public static void Main(string[] args)
{
for(int i=0; i<1000; i++)
locks.Add(new object());
var tasks = new List<Task>();
for(int i=0; i<15000; i++)
{
var t = Task.Run(() =>
{
int file = (new Random()).Next(0, 1000);
object l;
lock(_list_lock)
{
l = locks[file];
}
lock(l)
{
//write to file 'file'
}
});
tasks.Add(t);
}
tasks.ForEach(f => f.Wait());
}