Concurrency Flashcards
Why is concurrency difficult for programmers to reason about?
Because it breaks our expectations of how code works ie,
Instructions are atomic
The order in which instructions are executed are fixed
These two assumptions are broken by concurrency
What is concurrency?
Running parts of the same computation at the same time
What are Threads?
Independent streams of computation
Implicit Interactions - Shared memory between threads, can read and write the same data
More co-ordination required
What are Processes?
Each process has its own internal state
Explicit Interactions: message passing
Less co-ordination needed
What is compiler based concurrency?
Don’t implement any concurrent operations yourself, and let the compiler figure it out for you :)
What is a pre-protocol?
Making sure that no-one else is using the critical section
before a thread enters it,
and marking the critical section as in use
What does a Lock do?
Controls access to the critical section
Lock() - Asks for access to a critical section
Goes to sleep if CR occupied
Unlock() - Leaves Critical Section
Wakes up thread sleeping on this lock
What is one benifit of Sleep in Locks?
Avoids starvation
What is a Remote Procedure Call?
When the Sender waits for the Receiver to REPLY
i.e. Solidity
What are Synchronous Messages?
This is where the Sender waits for the Receiver to GET the message
What are Asynchronous Messages?
This is where the Sender sends the message and continues execution immediately
What are the properties of Remote Procedure Call (RPC)?
Simpler to reason about
Little concurrency
Send and Reply are atomic
What are the properties of Synchronous Messaging?
Lot of communication overhead
When send() returns we know the message was received
Send and reply are atomic, but the state may have changed between them
What are the properties of Asynchronous Messaging?
Not easy to reason about
Little Communication overhead
No guarantee the message will be received
Buffers needed for managing messages
Why is Automatic Concurrency easy for Functional Programming?
Functions have no explicit ordering
Functions have no state
Data flow is explicit