Concurrency Overview
Single threaded Concurrency
-
Each activity requires a separate process
- Has one execution path or "thread" through the program's code
-
May need "interprocess communication"
- Message queue, pipe, semaphore, shared memory, network socket, etc
-
Each process has its own private memory space
- Cannot accidentally alter another process's data
-
Process can be run on different computers over a network
-
Creating a new process can be slow on some systems
-
IPC
- Adds complication
- Can be slow to set up
- No direct support in C++
Multi threaded Concurrency
- A single process performs all activities
- Each activity has its own execution path or "thread"
- Concurrency is achieved by starting a new thread for each activity
Threads
-
Each thread is an independent execution environment
- Has its own execution stack
- Has its own processor state
-
Threads are "light weight processes"
- Less overhead to start up
- Smaller task switching overhead
- Easier to share data between threads
-
All the threads in a process share its memory space
- Can access global data
- Pointers can be passed between threads
-
Lack of data protection between threads
- Can cause inconsistency and data corruption
- Requires careful programming
Advantages of Concurrency
-
Improves responsiveness of the program
- The user is never left staring at a "stuck" program
-
Improves throughput
- Processing large amounts of data in parallel takes less time
-
Allows separation of logically distinct operations
- e.g. mail program starts new threads
- Compose an email
- New fetch messages etc
-
Takes full advantage of modern hardware
- Threads can be distributed among processor cores
Disadvantages of Concurrency
-
Adds complexity to programs
- Code is harder to write and harder to understand
- Bugs are more likely
-
May not result in faster programs
- Data protection overhead
- Thread coordination overhead
-
Use only when the benefits outweigh the costs