Preemptive Threads with Shared Memory
Last updated at 2:19 pm UTC on 16 April 2005
A common program architecture is to form multiple threads and then have them collaborate via shared memory.
One benefit of this approach is the initial simplicity of the program. Simply fork and then write each thread and you are "done".
A major challenge comes when you try to prevent threads from accessing data when it is in an inconsistent state. If you don't take any care, then one thread might read from memory that another thread is currently writing to. To prevent this, one can use locks in order to arbitrate .
Locks bring their own problems, however. First, it is rarely clear that enough locking has been added to a program that all possible executions will be safe. Second, it is rarely clear that there is a low enough amount of locking that the program will avoid deadlock.
Finally, this architecture suffers from giving non-repeatable executions. This means that a program may perform perfectly well on a developer's machine but then act wrong on a user's machine. It also means that a program my fault on only one execution in one thousand, which causes debugging to become more difficult.
An alternative architecture is the Event-Loop Architecture.