Monday, August 11, 2008

Notes to self - Producer/Consumer pattern

Short note to self for implementing the producer-consumer pattern

Structure to use for the internal "queue".

The right structure to use for the internal work item storage is queue and not stack based. In case when producers are fast they will always top the LIFO structure with work items so consumers will pick up first x from the top. That has got serious consequences in holding up transactional resources by increasing the average time of dispatch (across items) from the in-memory "storage". Plus bottom items may be picked after their transaction timeout has ended.

If still index-access structure is required (when internal array is used as a submission queue as well) then

Notification on unit of work.

When using Monitor.Pulse, Monitor.Wait for prompt dispatching and synchronization. The start time of the producer and consumer should be taken into consideration, if producer issues any Monitor.Pulse before consumer does Monitor.Wait, those Pulse are going to be lost. It would be better not to use this mechanism at all.

This is all trivial, just making notes to self.

No comments: