Sunday, March 28, 2010

Application patterns

Creating big applications is a long and difficult process. It is also very vague and non-linear. Design documents change as per the fluctuating needs of the client, limitations of the development environment and programming language, paucity of time and funding, and of course, developer competence. Before one sits down to start writing code, one must have a conceptual overview of the application. This is the architecture phase. Once this conceptual overview has been fleshed out, one starts thinking about the patterns, data types and algorithms to be used. Note that by patterns I mean application patterns and by data types, I mean abstract data types and not data structures. A queue or a stack is an abstract data type and NOT a data structure, queues and stacks can be implemented using either arrays, linked lists or heaps. These are data structures. Data types provide intent, while data structures provide the means. A pattern I want to discuss is the Producer/Consumer queue.
This pattern is useful if you have jobs that are generated which need to be executed in a sequential manner. So we have a queue of tasks and a worker thread that executes those tasks on-the-fly as they are enqueued in the queue. So how is it implemented? Very straightforward. We have a queue, a locking object, pool of threads and a thread monitor. To enqueue, first lock the locking object and call pulse in the monitor. To consume, simply lock the queue, dequeue, and pass on the task to a waiting thread. To dispose simply enqueue NULL in the queue and wait for the threads to terminate.

No comments: