Design Patterns – What is the ‘Worker Object Pool’ Pattern?

cdesign-patterns

In my application requests are processed by objects of Processor-derived classes, something like CreateItemProcessor or MoveItemToGroupProcessor. Base Processor class implements most of common processing logic, and derived ones make some specific business logic related tasks.

An object of a certain processor type is created before request processing, and then wiped out afterward.

Eventually it came to a situation, when creation of a single processor object was taking major part of request processing time. So now I'm thinking about creation of a pool of request processor objects, and reuse them instead of using create/delete approach.

The problem here is that any single Processor object is not thread-safe, and actually shouldn't be tread-safe: it stores request-specific data inside. So my general approach to that is as follows:

  • Try to acquire 'processor' from a pool;
  • If there are no any available, wait;
  • When we have processor available, mark that as 'working' and start processing;
  • When job is done, "return" processor to the pool;
  • Notify pool that free processor is available.

Is there a kind of design pattern for that? Am I missing something from the existing GoF patterns? Any C#-related implementation details?

Best Answer

Yes there is, and it is named (rather unsurprisingly) Object Pool :-)

Googling for a C# implementation pops you up a StackOverflow thread in the first place.

Related Topic