Shaun Abram
Technology and Leadership Blog
Code Camp: The Basics of Threading
The Basics of Threading
Speaker: Kim Greenlee
Started with an overview of Threads and the concept of a process space
- A Process consists of :
- Heap
- Code
- Multiple Threads, each with a stack and registers
- Each app runs in its own process space
- Every process space gets at least one thread
- Each process space has a heap and code
- Process space creation is heavy (resource intensive)
If you want to share data between the process space, need some way to explicitly do it
i.e. need a way to allow different process spaces to share their heap and code
She talked about Replay Solutions (http://www.replaysolutions.com/) – who provide thread monitoring/debugging tools.
Issues with Threading
- Race condition – who gets there first
- Deadlock – one thread wating forever on another (e.g. thread A has x and needs y; Thread B has y and needs x)
- Starvation – Pick me! I need a turn
- Livelock – you go, no you go, no I insist you go… (Thread A has x and needs y; Thread B has y and needs x; So Thread A out x back, tries whole thing again, but with no success?) [Didn’t quite understand this one]
- Memory reordering – things aren’t always what they seem. [Didn’t quite understand this one]
Thread Costs (in .Net)
- Creation costs = 200,000 cycles
- Destruction Cost: 100,000 cycles
- i.e. super high!
Decomposition
- Task decomposition – break the task down in to multiple parts. e.g. do the slow, file based parts in a separate thread
- Data decomposition – break the data up in to chunks
- Messaging – passing messages back and forward? Rarely used outside of scientific circles. e.g. used in physics/fluid dynamics tests
Tips and Tricks
- Implementation Plan
- Plan for problems – Log files – i.e. have a logging system built in
- Testing Strategies –
- debug in AND out of debugger as using debugger may cause different behaviour
- Test on single and multi-processor testing
- Code Review – pair programming
- 3rd party tools – coverity? Replay solutions; Intel;
Overall:
A good overview/intro to threading. Kim clearly has a lot of hands of experience. All examples were in .Net/C#/VisualStudio, would have preferred Java, but obviously that just my personal bias.
Links
http://krgreenlee.blogspot.com/