1 / 21

K.TOP 2005

K.TOP 2005. Multi-Thread 2005/1/29. Delphi Win32 API 版僕 SouthWind. Multi-Thread 概述 (1). Time Slice Schedule. Code Execute. Multi-Thread 概述 (2). Process. Global. Heap. Multi-Thread C ommunication. Global 變數. Signal. Message. Multi-Thread 難題. Racing Condition Dead Lock.

richelle
Download Presentation

K.TOP 2005

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. K.TOP 2005 Multi-Thread 2005/1/29 Delphi Win32 API 版僕 SouthWind

  2. Multi-Thread 概述 (1) Time Slice Schedule Code Execute

  3. Multi-Thread 概述 (2) Process Global Heap

  4. Multi-Thread Communication Global 變數 Signal Message

  5. Multi-Thread 難題 • Racing Condition • Dead Lock

  6. Racing Condition 原因 • Global Resource • 非 Atomic Execute

  7. 避免Racing Condition • Protect Global Resource • Synchronization

  8. Synchronization Technology : • Mutex • Critical Section • Semaphore • Event • Interlocked or Atomic API • Spin Lock • Wait For Object API

  9. Dead Lock 原因 • 設計不良。 • 不可預期的異常發生。

  10. 避免Dead Lock • Time Out 機制 • 清晰的邏輯 • Dead Lock detection

  11. Delphi & Multi-Thread • VCL • Synchronize()

  12. VCL • 絕大多數的 VCL 元件,都是 thread none-safe • 需要自行設計同步機制保護。

  13. Synchronize() • 利用 Message 的技巧,使得 Synchronize() 可以切換至 Main Thread 執行。 • 快速方便的同步機制

  14. Synchronize() 的誤用 • Synchronize() 內執行過長的 Code,造成Thread 效益消失或效率不彰。 • Synchronize() + Wait For Object 系列 API,容易產生 Dead Lock。

  15. Multi-Thread 設計小建議 • 深刻的了解 Multi-Thread 原理與機制。 • 熟悉善用各種同步機制。 • 謹慎使用 Synchronize() ,尤其注意有 Wait For Object 系列 API。 • 分析同步區執行時間的長短,以決定最佳的同步機制。

  16. Win32 API (Multi-Thread) • CreateThread • SuspendThread • ResumeThread • TerminateThread • SetThreadPriority • WaitForSingleObject • WaitForMultipleObjects

  17. Win32 API (Synchronization) • InitializeCriticalSection • EnterCriticalSection • LeaveCriticalSection • DeleteCriticalSection • CreateMutex • ReleaseMutex • CloseHandle

  18. Win32 API (Synchronization) • InterLockedIncrement • InterLockedDecrement • InterLockedExchange • InterLockedExchangeAdd • CreateSemaphore • ReleaseSemaphore • CloseHandle

  19. Delphi & Multi-Thread • TThread Class • TThread.Suspend Method • TThread.Resume Method • TThread.Terminate Method • Synchronize()

  20. Reference • Win32 API 系統程式實例入門 (博碩) • Inside VCL (旗標)

  21. 敬請指教 野人獻曝,若有謬誤,請各位先進不吝指導。

More Related