Presentation on theme: "K.TOP 2005 Multi-Thread 2005/1/29 Delphi Win32 API 版僕 SouthWind."— Presentation transcript:

 Presentation on theme: "K.TOP 2005 Multi-Thread 2005/1/29 Delphi Win32 API 版僕 SouthWind."— 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) Stack Thread Global Heap Stack Thread Stack Thread Process

4 Multi-Thread Communication Stack Thread Global 變數 Stack Thread Stack Thread MessageSignal

5 Multi-Thread 難題  Racing Condition  Dead Lock

6 Racing Condition 原因  Global Resource  非 Atomic Execute var4 = (var1 + var2) * var3 mov eax,var1 add eax,var2 mul var3 mov var4,eax

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  Non-UI VCL  UI VCL  絕大多數的 VCL 元件, 都是 thread none- safe  需要自行設計同步機制保護。

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

14 Synchronize() Road Map

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

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

17 Win32 API (Multi-Thread)  CreateThread  SuspendThread  ResumeThread  TerminateThread  SetThreadPriority  WaitForSingleObject  WaitForMultipleObjects

18 Win32 API (Synchronization)  InitializeCriticalSection  EnterCriticalSection  LeaveCriticalSection  DeleteCriticalSection  CreateMutex  ReleaseMutex  CloseHandle

19 Win32 API (Synchronization)  InterLockedIncrement  InterLockedDecrement  InterLockedExchange  InterLockedExchangeAdd  CreateSemaphore  ReleaseSemaphore  CloseHandle

20 Win32 API For Synchronization Example Download Demo Program From K.TOP :  Racing Condition  Critical Section  Mutex  Semphore

21 Delphi & Multi-Thread (1)  TThread Class  TThread.Suspend Method  TThread.Resume Method  TThread.Terminate Method  Synchronize()  [threadvar]

22 Delphi & Multi-Thread (2)

23 Delphi K.TOP  【 Delphi 】【發表】 Multi-Thread 簡報 Demo 範例程式 http://delphi.ktop.com.tw/topic.asp?TOPIC_ID=64439 【 Delphi 】【發表】 Multi-Thread 簡報 Demo 範例程式  Multi-thread / Process 的工作分配 [ 利用 Timer 與 Semaphore] http://delphi.ktop.com.tw/topic.asp?TOPIC_ID=62971 http://delphi.ktop.com.tw/topic.asp?TOPIC_ID=62971  【問題】多執行緒 ( 均帶無窮迴圈 ) 能同時執行嗎 ? http://delphi.ktop.com.tw/TOPIC.ASP?TOPIC_ID=59940 http://delphi.ktop.com.tw/TOPIC.ASP?TOPIC_ID=59940  【 Delphi 】【問題】 WaitForMultipleObjects 的問題.. http://delphi.ktop.com.tw/TOPIC.ASP?TOPIC_ID=50545 【 Delphi 】【問題】 WaitForMultipleObjects 的問題..

24 Reference  Win32 API 系統程式實例入門 ( 博碩 )  Inside VCL ( 旗標 )  Programming in Delphi

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

留言

這個網誌中的熱門文章

curiously recurring template pattern delphi generics TLIST TBASE

algodaily alternative essential algorithms data structures daily coding inspired lintcode

toolkit uml generator code orm object-relational mapping er diagram class diagram synchronization