I have multiple processor objects which I use with ThreadPool to use them in a paralel process. Which processor I am going to use is basically depend on the incoming data and there might be more than 2000 different types; so as soon as my app runs it creates 1-2K number of processors in a dictionary and run the one I need according to incoming data in ThreadPool. Any process does not take more than a millisecond btw.
My psuedo code for running in the ThreadPool is below:
public void onIncomingNewData(rawData)
{
if (!ThreadPool.QueueUserWorkItem(processors[rawData.Type.Id].Process, rawData))
{
Console.WriteLine("Work item cannot be queued!");
}
//Thread.Sleep(1);
}
My problem here is synchrnozing the Processer objects; they have their internal cache and I dont want multiple threads running the same Process objects process method.
At the moment I am using "lock" inside of the process method and some other private methods that process method calls. But what would be the best approach to do this?