Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Threads, Critical Sections, and Termination

206 vues

Publié le

by Martin McClure
Correctness of a multi-threaded program is much more difficult to achieve than correctness of a single-threaded program, but in some situations we can't avoid using more than one thread, and therefore have to deal with the resulting complexity. In this talk, Martin will present some of the threading problems he's seen and solutions to those problems, with a focus on the additional
problems that arise when threads are terminated.

  • Soyez le premier à commenter

  • Soyez le premier à aimer ceci

Threads, Critical Sections, and Termination

  1. 1. Martin is getting the projector to work with his laptop.
  2. 2. Threads, Critical Sections, and Termination Martin McClure
  3. 3. Thread vs. Process
  4. 4. Shared State
  5. 5. ...sigh
  6. 6. GemBuilder® for Smalltalk
  7. 7. GBS
  8. 8. Mapping Dictionaries
  9. 9. ClientMap • Identity Dictionary •~2M keys •~100K updates/s •~(1-10)×100K lookups/s
  10. 10. 32-bit VW
  11. 11. 32-bit VW 214 hash values
  12. 12. Safe sharing of this structure?
  13. 13. Semaphore
  14. 14. Semaphore ...performance?
  15. 15. Performance
  16. 16. "Premature optimization is the root of all evil." --Donald Knuth
  17. 17. Simplicity Clarity
  18. 18. Performance
  19. 19. Performance 5ms
  20. 20. Semaphore ...performance?
  21. 21. Time microsecondsToRun: [1000000 timesRepeat: []]
  22. 22. Time microsecondsToRun: [1000000 timesRepeat: []] 3ns-4ns
  23. 23. [] 3ns-4ns
  24. 24. [sem critical: []]
  25. 25. [sem critical: []] ~70ns
  26. 26. [dict delegateAtClient: key]
  27. 27. [dict delegateAtClient: key] ~40ns
  28. 28. [sem critical: [dict delegateAtClient: key]]
  29. 29. [sem critical: [dict delegateAtClient: key]] ~130ns
  30. 30. Batched semaphore for modifying
  31. 31. No semaphore for lookup
  32. 32. Hash Bucket Lookup index := linear search for key. ^index == 0 ifTrue: [nil] ifFalse: [value := self basicAt: index. (keys at: index) == key ifTrue: [value] ifFalse: [nil]]
  33. 33. Hash Bucket Add index := freeHead. [] ensure: [freeHead := keys at: freeHead. self basicAt: index put: value. keys at: index put: key. size := size + 1].
  34. 34. ...but there’s a hole
  35. 35. Hash Bucket Lookup index := linear search for key. ^index == 0 ifTrue: [nil] ifFalse: [value := self basicAt: index. (keys at: index) == key ifTrue: [value] ifFalse: [nil]]
  36. 36. Hash Bucket Add index := freeHead. [] ensure: [freeHead := keys at: freeHead. self basicAt: index put: value. keys at: index put: key. size := size + 1].
  37. 37. [“empty block”] ensure: [“non-empty block”].
  38. 38. Hash Bucket Add index := freeHead. [] ensure: [freeHead := keys at: freeHead. self basicAt: index put: value. keys at: index put: key. size := size + 1].
  39. 39. Semaphore critical: mutuallyExcludedBlock self waitIfCurtailedSignal. ^mutuallyExcludedBlock ensure: [self signal]
  40. 40. testTerminateInEnsure | process count random delay | random := Random new. 10 timesRepeat: [process := [count := 0. [] ensure: [10 timesRepeat: [count := count + 1. 1000000 timesRepeat: [12 factorial]]. count := count + 1]] forkAt: Processor activeProcess priority - 1. delay := (random next * 100) asInteger + 10. "avoid 0-ms delay" (Delay forMilliseconds: delay) wait. self assert: process isTerminated not. process terminate. process priority: Processor activeProcess priority + 1. self assert: process isTerminated; assert: count equals: 11]
  41. 41. Terminating in a Critical Section
  42. 42. Hash Bucket Add index := freeHead. [] ensure: [freeHead := keys at: freeHead. self basicAt: index put: value. keys at: index put: key. size := size + 1].
  43. 43. Is this the best way?
  44. 44. Other Ways • Semaphore •#valueUninterruptably •#valueUnpreemptively
  45. 45. Semaphore
  46. 46. Semaphore •No termination protection
  47. 47. Semaphore •No termination protection •70ns
  48. 48. #valueUninterruptably
  49. 49. ~87ns []valueUninterruptably
  50. 50. #valueUnpreemptively
  51. 51. []valueUnpreemptively ~175ns
  52. 52. [] ensure: []
  53. 53. [] ensure: [] ~15ns
  54. 54. Final Thoughts
  55. 55. ?
  56. 56. Threads, Critical Sections, and Termination Martin McClure

×