I have a piece of code that is executed by n threads. The code contains,
for(;;)//repeat about 10,000 times
{
lock(padlock)
{
File.AppendAllText(fileName, text);
}
}
Basically, all threads write to the same set of 10,000 files and hence the files are the shared resource. The issue is that the 10,000 open,write,close performed by each thread is slowing down my program considerably. If I could share the file handlers across threads, I’d be able to keep them open and write from different threads. Can someone throw some light on how I can proceed?
Let all threads write to a syncronised list.
Let one thread ‘eat’ the items on the list and write them with a single FileWriter to the file.
Presto problem solved in exchange for some extra memory usage.