Reputation: 2503
The central process for an app (in C#) I have written involves potentially many reads from a disk file and then processing those reads. The data is then written out via the BinaryWriter
to the same output file.
The process is often very lengthy so I thought, if it is running on a multicore machine, it could instantiate a thread for each read up to cores x processors threads. each one would process the data and then write it out. There are no critical sections except the BinaryWriter
.
I've noticed that when I the app on my machine with 4 cores taskmgr says it is using 25% of the CPU so I am guessing it is using 100% of one of the cores and the app could perhaps greatly benefit from using the other 3 cores as well.
If I use a class Working
and a function Working.work()
to do the processing and I have four cores to work with (and of course that 4 would not be hard coded; I'd interrogate to see how many I have and use that number what would I do?
Currently I have something like:
Working working = new Working();
while (bytesRead = binaryReader(ref buffer,0,bytesToRead) != 0)
{
work.buffer = buffer;
working.work();
binaryWriter(buffer,0,buffer.Length);
}
It seems it would help to move this all to a thread. But I have to know when the threads are finished so the new read can be on a thread.
Upvotes: 0
Views: 593
Reputation: 19
There are some good examples of asynchronous file IO and using tasks on MSN here: http://msdn.microsoft.com/en-us/library/jj155757.aspx
In general, what you can do is make an asynchronous worker Queue (note: since .NET 4.0 there's a ConcurrentQueue collection that really helps facilitate asynchronous processing) of work items that you set an x-amount of tasks working at. Finished tasks you would then move to a non-asynchronous writer queue that you setup a different task for that awaits for new items to arrive.
See also more from Microsoft on this topic here: http://msdn.microsoft.com/en-us/library/hh873173.aspx
Especially from the Producer / Consumer pattern downwards should be relevant to you.(though I don't see it using ConcurrentQueue, perhaps because that was written before 4.0?)
Upvotes: 1