Notes
Slide Show
Outline
1
Synchronization
2
Sychronization
  • Contents
    • Mutexes
    • Synchronized Interface
    • Sequential Operations
    • External Locking
    • Auto Locking
    • Sychronizing Smart Pointer
    • Guarded Operations
    • Internal Locking
    • Execute Around Pattern
3
Mutex
  • Common synchronization primitive
    • intra-process, inter-process
4
Generalised Synchronization
  • Synchronization primitives share intent
5
Terminology: Sequential Operation
  • Concurrent callers synchronize operation access
6
Weak External Locking
7
Weak Solution
  • Why
    • try/catch/throw not
      in embedded C++
    • Bloat, more code
      than before
    • Duplication, must be
      repeated every time
    • Complexity, code is
      harder to follow
    • Responsibility, is
      with callers
    • Scalability, is poor
    • Intrusive
8
Auto Lock
  • “Resource Release in Finalisation” idiom
9
Stronger Solution
  • Why?
    • No try/catch/throw
    • No bloat
    • No duplication
    • Not complex
    • Non intrusive
    • Scalable, easy to lock statement sequences
  • But...
    • Responsibility is still with callers
    • User must perform two actions
10
Alternative Solution
  • Synchronizing smart pointer
11
Synchronized Queue Pointer
  • Can be refactored into template class
12
Synchronized Queue Pointer
  • Implementation
13
Temporary Lock
  • Can be nested inside synch_ptr class
14
Temporary Lock 'tors
  • Copy c'tor?, copy assignment?
15
Temporary Lock Access
  • Single statement granularity
    • Don't use twice in same statement
    • Non re-entrant lock will cause deadlock
16
Terminology: Guarded Operation
  • Operation synchronizes access to itself
17
Weak Internal Locking
18
Stronger Internal Locking
19
Safe Solution
  • Because...
    • Encapsulated resource
    • Simple model of use
  • But...
    • Locks at single
      operation level
    • Sometimes need
      coarser granularity
    • Eg, what you want to
      enqueue a sequence of
      tasks operations?
20
Flexible Solution
  • Execute Around + Command pattern
21
Flexible Solution
  • Execute Around + Query pattern
22
Concrete Command Class
23
Concrete Command Object