Concurrency patterns
The named patterns for concurrent and networked programs: the POSA2 event handling, synchronization and concurrency patterns, the patterns of *Java Concurrency in Practice*, and the other common ones. The mechanisms they use are in concurrency-and-parallelism.
Concepts
- Concurrency pattern
A concurrency pattern is a design pattern for work that runs at the same time. Each one answers the same question: which code may touch which state, and when.
core
Patterns
- Active Object
Active Object gives an object its own thread. A proxy turns each method call into a request on a queue and returns a future, and a scheduler runs the requests on the object one at a time.
core - Concurrent pipeline
A concurrent pipeline is a chain of stages that run at the same time and pass values through channels. A slow stage can have several workers, and full buffers make fast stages wait.
core - Copy-on-write
Copy-on-write shares one copy of data among its users until one of them writes. The writer makes a private copy and changes that, so readers keep a stable snapshot and need no lock.
core - Event loop
An event loop is one thread that takes events and callbacks from a queue and runs each one to the end. It handles many tasks without locks, if no callback blocks the loop.
core - Fork/Join
Fork/Join splits a task into parts that can run in parallel, and then joins them, so that only one thread continues with the combined result. Recursive splitting gives parallel divide and conquer.
core - Future as a design pattern
A call starts slow work and returns a future at once. The caller does other work and asks the future for the value when it needs it, and it waits only if the value is not ready.
core - Guarded suspension
Guarded suspension makes a method wait until it holds a lock and its precondition is true. The thread sleeps on a condition variable and checks the guard again in a loop each time it wakes up.
core - Immutable object
An immutable object cannot change after its constructor ends. Threads can share it and read it with no lock, and an update makes a new object instead of a change to the shared one.
core - Master-worker
In master-worker, a master splits a job into parts, gives the parts to identical workers that run at the same time, and combines their results into the final result.
core - Monitor Object
Monitor Object makes an object safe for many threads. Each method holds the object's lock while it runs, and a method that cannot go on waits on a condition that releases the lock.
core - Producer-consumer
Producers put items into a shared buffer and consumers take them out. A bounded buffer makes a producer wait when it is full and a consumer wait when it is empty, so neither side runs too far ahead.
core - Reactor
A reactor waits for events on many handles in one thread and calls the handler that is registered for each handle, so one thread serves many connections.
core - Read-write lock
A read-write lock lets many threads read shared data at the same time but gives a writer exclusive access. Its priority policy decides if waiting readers or a waiting writer go first.
core - Scoped Locking
Scoped Locking takes a lock when control enters a scope and releases it when control leaves the scope by any path, so no return or exception can leave the lock held.
core - Thread confinement
Thread confinement keeps mutable data reachable from one thread only. No other thread can read or write it, so the data needs no lock. Local variables, UI threads and ownership in Rust use this idea.
core - Thread pool
A thread pool keeps a fixed set of worker threads that take tasks from a queue. The program pays for thread creation once and limits how many tasks run at the same time.
core - Thread-Specific Storage
Thread-Specific Storage gives each thread its own copy of an object behind one global access point. The code reads and writes the object with no lock, because no other thread can reach that copy.
core - Work queue
A work queue holds tasks that several workers take and do. The queue keeps each task until a worker acknowledges it, so a task whose worker fails goes to another worker.
core - Acceptor-Connector
Acceptor-Connector puts the code that sets up a connection in an acceptor or a connector, and the code that uses the connection in a service handler.
advanced - Asynchronous Completion Token
An asynchronous completion token is a value that a client sends with each asynchronous request and that the service returns unchanged, so the client can match each reply to its request.
advanced - Balking
A balking method runs its action only when the object is in the right state. In any other state it returns at once, or throws, and does not wait for the state to change.
advanced - Barrier synchronization
A barrier makes each thread of a group stop at one point until all the threads of the group arrive. Then all continue, so the work runs in phases and each phase sees the results of the last one.
advanced - Double-Checked Locking
Double-Checked Locking tests a condition before it takes a lock and again after, so code that must run once takes the lock only on the first calls. The plain form needs a safe memory model.
advanced - Half-Sync/Half-Async
Half-Sync/Half-Async splits a concurrent system into layers. Short handlers react to events and never block, threads run the long tasks with blocking calls, and a queue joins the two layers.
advanced - Leader/Followers
Leader/Followers lets a pool of threads take turns on one set of event sources. The leader waits for an event, promotes a follower to lead, and then handles the event in its own thread.
advanced - Proactor
A proactor starts asynchronous operations and, when each one completes, calls its completion handler, so the program reacts to finished work and not to readiness.
advanced - Scheduler
A scheduler object decides which waiting thread uses a shared resource next. Threads call enter and done, and one policy, such as priority or first come first served, sets the order.
advanced - Strategized Locking
Strategized Locking makes the lock of a component a parameter, so one implementation serves single-threaded and multi-threaded use with the lock that fits each case.
advanced - Thread-Safe Interface
Thread-Safe Interface lets only the public methods of a component take its lock, and they call private methods that never lock, so calls inside the component cannot self-deadlock.
advanced