C++ Threading: Tim Bailey
C++ Threading: Tim Bailey
Tim Bailey
• Simple solution:
• Put mutexes around ALL accesses to shared
objects
• Problems
• Must remember to lock every time
• Must associate correct mutex with the data
• Threading is mingled throughout the code
• Condition variables
• Wait condition, allows a thread to block
• Signal from another thread unblocks waiting
thread; the two threads are synchronised
• [see condition.cpp, wrapping pthreads]
• Unidirectional channels
• Thread safety encapsulated within channel
• Permits localised thread debugging
• Interface looks like single-threaded code
• Lots...
• Read-write locks, try locks, spin locks, timeouts
• Re-entrant functions; avoid global and static variables
• Deadlock prevention/detection
• Thread priorities and scheduling
• Priority inversion
• Patterns
• Producer-Consumer
• Thread pools
• Active objects with Futures
• Publish-Subscriber
• Thread lifetimes
• Thread termination; graceful
• Detached, non-detached, joining
• Threads and exceptions
• Channel varieties
• Blocking, buffering, one-to-many, many-to-one
• Name services
• IDs of active threads, classes and objects
• Dynamically connecting/disconnecting channels at runtime
• Connection requests/notification
• Simplicity is key