Greg Heo - @gregheo - from Swift Unboxed
- Because we want:
- Programs that seem faster to the user.
- Programs that are easy to understand for the programmer.
- Structuring our programs better
- Thinking in terms of tasks
- Protecting resources
- Concurrency vs Parallelism
- "Concurrency is the composition of independently executing processes." (- Rob Pike)
- "Parallelism is the simultaneous execution of computations."
- "Concurrency is about ** dealing ** with lots of things at once. Parallelism is about ** doing ** lots of things at once."
- Order? Shared state? Mutable state?
We use threads to model our tasks.
var tProcessData: pthread_t?
pthread_create(&tProcessData, nil, processData, nil)
pthread_join(tProcessData!, nil) //This will block until the work is complete.
- The concept is:
- Create a thread
- Run some code
- Wait for it to complete
We use locks to model our access to resources.
- Locks are all about protecting access to code.
let lock = NSLock()
- A lock is is like the conch shell that somebody has to be holding to talk. It's a token that means only one thing can access a property at at time.
There are two different uses for locks:
- Locks as signals (wait until A is done to start B)
- Locks as mutual exclusion (protect access to shared state from multiple threads)
// Sorry, I got really behind the presenter at this point. Notes end here.