[Pdx-pm] queueing, avoiding race conditions

Michael G Schwern schwern at pobox.com
Wed Aug 10 15:41:14 PDT 2005

On Wed, Aug 10, 2005 at 11:49:20AM -0700, Randall Hansen wrote:
> roughly:
> - Widget updater "A" begins
>      - IF another Widget update ("B") is requested
>      - request "B" is queued
> - process "A" completes
> - process "A" examines the queue
>      - IF it finds request "B"
>      - goto 1
> - end

If I understand the problem properly, here's a very simple solution.

Make a directory: queue/
Stick one file into queue/ for each request.

Your processor reads the files in the queue.
It works through them in filename order.
Before processing a request, check if its read locked.  If it is, don't 
  process it.
When you begin work on a request, read lock the file.
When you're done processing, delete the file.
Re-read the files in queue/ in case more have been added.
Continue processing until there are no more unlocked files.

This should work well no matter how many processors you have or when requests
are added.  It also works well with pipelining.  If each request has to go 
through several processes instead of deleting a request on completion you 
just move it to another queue.

Michael G Schwern     schwern at pobox.com     http://www.pobox.com/~schwern
Ahh email, my old friend.  Do you know that revenge is a dish that is best 
served cold?  And it is very cold on the Internet!

More information about the Pdx-pm-list mailing list