Concurrency Fundamentals
Concurrency is the structuring of a program so that multiple tasks can make progress within overlapping time periods. It covers threads, synchronization primitives, race conditions, deadlocks, and the models programmers use to coordinate shared mutable state safely.
itComputer fundamentals | OpenSkills.info
Intro
Concurrency Fundamentals
Concurrency lets a program manage several independent activities whose lifetimes overlap. Parallelism means computations execute at the same instant. A concurrent program can run on one processor by taking turns. A parallel program needs execution capacity for work to run simultaneously.
You meet concurrency whenever software must remain responsive while waiting, serve many requests, process background jobs, or use several processor cores. The benefit is not automatic speed. Concurrency gives you a way to structure overlapping work. Parallelism can reduce elapsed time when the work and hardware allow it.
The central difficulty is order. A single sequence of instructions has one obvious next step. Concurrent tasks can interleave in several valid orders. Your design must remain correct for every order the runtime permits, not only the order you saw during one test.
Units of concurrent work
A process is a running program with its own operating-system resources and address space. A thread is an execution path within a process. Threads in one process commonly share memory. A runtime may also offer lighter units such as tasks, futures, coroutines, or goroutines.
An asynchronous task can pause while it waits and let other tasks use the same thread. This cooperative style works well for workloads with many waits, such as network services. A task must reach an await or another yield point before peer tasks can run on that event-loop thread.
Continue the course
This section is part of the paid course.
See pricing to subscribe, or log in if you already have access.
Sources
- https://go.dev/blog/waza-talk
Supports
- Concurrency composes independently executing activities
- Parallelism is simultaneous execution of computations
- Concurrency and parallelism are related but distinct
- https://go.dev/ref/mem
Supports
- A data race involves conflicting access to the same memory location without the required synchronization
- Happens-before combines sequenced and synchronized ordering
- Channels, mutexes, initialization, and atomic operations provide documented synchronization effects
- Data-race-free Go executions have sequentially consistent outcomes
- https://docs.oracle.com/javase/specs/jls/se26/html/jls-17.html
Supports
- A monitor permits one thread at a time to hold its lock
- Incorrect synchronization can produce counterintuitive shared-memory observations
- Happens-before includes program order and synchronizes-with ordering
- Conflicting accesses not ordered by happens-before form a data race
- Programs that acquire several locks need a deadlock-avoidance discipline
- https://doc.rust-lang.org/book/ch16-01-threads.html
Supports
- Threads are independent execution paths within a program and can run simultaneously
- Thread scheduling order is not guaranteed
- Threads introduce race-condition, deadlock, and reproducibility risks
- Joining waits for a spawned thread to complete
- https://doc.rust-lang.org/book/ch16-02-message-passing.html
Supports
- Channels transfer data between threads
- A blocking receive waits for a value while a nonblocking receive returns immediately
- Channel closure communicates that no more values will arrive
- Messages from concurrent producers can arrive in nondeterministic order
- https://doc.rust-lang.org/book/ch16-03-shared-state.html
Supports
- Shared-state concurrency allows multiple threads to access the same memory
- A mutex provides exclusive access to guarded data
- Lock acquisition can block until the mutex becomes available
- Scope-bound guards can release a lock automatically
- https://docs.python.org/3/library/asyncio-task.html
Supports
- Asyncio tasks schedule coroutines cooperatively
- An event loop runs one task at a time and runs other tasks when the current task awaits
- Task groups provide a structured way to wait for related tasks
- Cancellation, waiting, sleeping, and timeouts are explicit task operations
- https://docs.python.org/3/library/asyncio-queue.html
Supports
- An asyncio queue can have a maximum size
- Adding to a full bounded queue waits until space becomes available
- Queue task tracking can wait until queued work is processed
- https://docs.python.org/3/library/concurrent.futures.html
Supports
- Executors provide a high-level interface for asynchronous callable execution
- Thread and process pools use bounded sets of workers configured by the caller or implementation
- Waiting on dependent futures within a pool can deadlock
- https://go.dev/doc/articles/race_detector
Supports
- Go provides dynamic race detection through the race option
- Race reports include conflicting accesses and their creation stacks
- The detector finds races only on execution paths that run
- Running realistic workloads can increase dynamic coverage
