Lecture 6 - Concurrency Intro Flashcards
What is concurrency?
The ability of different program parts to be executed simultanously.
How many ways of having concurrency is there?
2
What are the 2 ways of having concurrency?
- shared memory (one memory location used by different parts of the program running at the same time)
- message passing (different memory locations, is more robust)
What does shared memory concurrency require?
Synchronisation between parts of the prgram which are to run alongside each other.
What is parallelism?
Simply about making your program faster. There is usually no need to have threads, it just so happens that a program can be run in parallel when there are mutliple processors.
How is concurrency different to parallelism?
Concurrency is a programming paradigm, where threads are used to get asynchronous events from the enviroment (no waiting on program, other threds can continue) or structuring program as a collection of interacting agents.
Concurrency has?
different independent processes being executed
- dealing with lot of things at once
What does parallelism have?
simulatnous execution of related computations
- doing lot of things at once
What is a process?
Process is a program with its own memory address space.
How many processes can be executed simulatneouly?
Many, CPU does it all the time.
What is a thread?
independent sequence of program instructions. a process can have multiple threads, sharing the same address space.
-> can be executed simultanously
-> essentially a subset of a process
Lifecycle of a thread:
- create
- start (possibly with arguments)
- each is given an identifier
-> can be used to kill / pause / interrupt - teminated
In shared memory how does communication between threads happen?
By modifying the shared memory space.
What are POSIX threads?
threading implementation in C
What flag has to be specified with the clang compiler to compile code with POSIX threads?
-lpthread
What is the posix thread create method?
pthread_create(thread , thread attr ,function , argument to function)
What is the posix thread join method?
pthread_join(thread, NULL (result))
What does pthread_create return when successfully creating a thread?
0 - refer to slide 8
What is pthread_Join do?
What does it return when successfully
Waits for a another thread to terminate before terminating.
Returns 0 when successful.
Refer to slide 9
What is assert?
Essentially a test, it will raise an exception when not met
What is mutual exclusion?
A problem in concurrency. 2 threads accessing the same variables in shared memory, leading to inaccurate results.
What does our code become if we do not control mutual exclusion?
our code becomes non-deterministic. The process is inconsistent
What is a critical region?
part of the code that updates shared variables or data structures. Mutual exclusion means only one thread ever executes this part of code
What is a race condition?
when the result of the program depends on the order in which the threads are executed
What is a lock?
What is another name for a lock?
Lock is something that needs to be obtained in order to execute critical region.
AKA Mutex
What must happen to the lock when thread is finished with a critical section?
It must be released
What are the semantics of acquiring a lock?
- if another thread owns the lock , the requesting thread is blocked until owning thread releases lock
- if lock is not owned then the requesting thread is granted ownership
What happens on released of a lock if there is blocked threads
The first blocked thread is automatically granted ownership
What do producers and conumers have to do when we have a bounded buffer?
The producer has to wait (is blocked) when buffer is full.
The consumer has to wait (is blocked) when buffer is empty.
Consumers and producers have to both work with a a lock to ensure consistency.
When unsafe, there could be gaps left in the buffer, leaving us with an impression that it is full, when it isn’t. etc.
Problem with having to wait for items to enter the buffer (consumer) while having the lock/
Since consumer is waiting for items to enter the buffer, while holding the lock, the producer is not able to add anything into the buffer. THIS IS DEADLOCK.
What is deadlock?
When a thread is holding a lock, without making progress, while also blocking other threads.
Potential solution to deadlock #1?
Releasing the lock when count is zero. This is via polling.
Wastes CPU time and resources.
AKA Busy Waiting
What is a pthread_mutex_t?
A lock or mutex
What is busy waiting?
Having a loop to continously check whether a condition has been met (keep having to unlock and lock mutexes).
Better solution to busy waiting?
Condition variable
Having a variable indicating whether a condition is ready. It must be changed by the thread currently holding the lock.
Implemented with:
pthread_cond_wait(&cv, &m)
pthread_cond_signal(&cv) -> assigns ownership to one of the waiting threads
Example of Condition Variable.
pthread_mutex_lock(&m);
while (!cond) {
pthread_cond_wait(&cv, &m);}
What are condition variables analogoues to?
hardware interrupts
The bounded buffer problem will have how many condition variables?
Two, one for add and one for remove.
What is a monitor
A class that provides safe access to a shared resource.
, Important coordination aspects:
- partitioning =determining which parts of the computation have to seperately evaluated
- placement = determining where threads should be executed e.g. allocated to least busy core
- communication = when to communicate and what data to send
- synchronisation = ensuring threads can cooperate without interference
What is a semaphore?
-holds an integer counter which provides 2 operations:
- wait, which decrements the counter and blocks
- signal, increments the counter and wakes waiting threds
What is a binary semaphone?
Semaphore holding 0 or 1.
If we use a semaphore, then do we need locks and cvs?
No, all handled by a semaphore, as the locks and cvs are built in.
What should a semaphonre structure contain?
mutex or lock
condition variable
current value
max value / min value
What do we need to avoid in concurrency?
- deadlock
- livelock
- starvation
What is livelock?
Threads run, but make no progress.
What is starvation?
Some threads never making any progress.
What do we need to do when programming concurrently?
must correctly lock , unlock and wait for safe access (via semaphores or condition variables)