I have a queue that is constantly being filled with work items
these items need to be processed in a timely manner – so we have many different processors waiting to receive the items that are being processed
Each of these sub processors can only handle one set of data at a time and the data needs to be pushed to it so this is how i was thinking about doing it
It starts off with:
- A Process inserts items into the queue
- The queue should be unique – so that if a item is already in the queue it should not be reinserted (I am not sure at this time – but i might need to boost the priority on such an item , in which case a queue might not work)
I was thinking
- Dispatcher creates a object for each available processor
- Start Loop:
- Dispatcher Checks if there are items in the queue to be worked on
- Dispatcher looks for a processor that is not currently working
- Dispatcher Sends the work items to the Idle Processor
- Goto Start Loop:
My question is , how do i manage the threads on the Processor Objects
I am thinking of doing one of the following:
- When a Processor object gets tickled by the dispatcher – Create a thread and start processing on it, when the thread finishes let it die. – Obvious Cons: Creating and killing a bunch of threads (Will a thread pool alleviate this issue?)
- Creating a dedicated thread for each processor that will do its work and then check the processor object to see if there is any more work for it to do – Obvious Cons: 50 Processors means 50 threads , and the synchronization might get difficult
- There is probably a simpler way to do this
Thank you
Edit
I seem to have been unclear – the data does not have to be processed by all processors
It can be processed by ANY of the processors – however once a processor start processing data we cannot send it any new data until it has finished processing – meanwhile new data is pouring into the queue and the manager needs to keep dolling out the data as fast as it can –
So the loop is like this again
- Dispatcher looks to see if any of the processors are available and there are items in the queue to process
- Dispatcher chooses a idle processor and pops the top data from the queue and sends it to that processor to process it
- the dispatcher waits a little bit and pools again
at the processor side – the data is sent to an external system ( a somewhat lenghty process) and the processor waits for the external system to return and it stores the processed data – only then is the processor available to except a new set of data off of the queue
The simplest way I have managed to do a similar queue management system is by using the built in Queue object in C#.
Now the message must be sent to each thread, and if you don’t really want to deal with synchronization, lock and pop the top object off. You could spool up a thread, or use an EventHandler. If using an EventHandler, you need to synchronize on both threads but it should be faster.