k.top 2005 multi-thread 2005/1/29 delphi win32 api 版僕 southwind

Post on 05-Jan-2016

227 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

K.TOP 2005

Multi-Thread2005/1/29

Delphi Win32 API 版僕

SouthWind

Multi-Thread 概述 (1)Time Slice Schedule

Code

Execute

Multi-Thread 概述 (2)

Stack

Thread

Global

Heap

Stack

ThreadStack

Thread

Process

Multi-Thread Communication

Stack

Thread

Global 變數

Stack

ThreadStack

ThreadMessageSignal

Multi-Thread 難題

Racing Condition Dead Lock

Racing Condition 原因 Global Resource 非 Atomic Execute

var4 = (var1 + var2) * var3

mov eax,var1add eax,var2mul var3mov var4,eax

避免 Racing Condition

Protect Global ResourceSynchronization

Synchronization

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

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

避免 Dead Lock

Time Out 機制 清晰的邏輯 Dead Lock detection

Delphi & Multi-Thread

VCL Synchronize()

VCL

Non-UI VCL UI VCL 絕大多數的 VCL 元件 , 都是 thread none-

safe 需要自行設計同步機制保護。

Synchronize()

利用 Message 的技巧 , 使得 Synchronize() 可以切換至 Main Thread 執行。

快速方便的同步機制

Synchronize() Road Map

Synchronize() 的誤用 Synchronize() 內執行過長的 Code, 造成

Thread 效益消失或效率不彰。 Synchronize() + Wait For Object 系列 API,

容易產生 Dead Lock 。

Multi-Thread 設計小建議 深刻的了解 Multi-Thread 原理與機制。 熟悉善用各種同步機制。 謹慎使用 Synchronize() , 尤其注意有 Wait

For Object 系列 API 。 分析同步區執行時間的長短 , 以決定最佳

的同步機制。

Win32 API (Multi-Thread)

CreateThread SuspendThread ResumeThread TerminateThread SetThreadPriority WaitForSingleObject WaitForMultipleObjects

Win32 API (Synchronization)

InitializeCriticalSection EnterCriticalSection LeaveCriticalSection DeleteCriticalSection CreateMutex ReleaseMutex CloseHandle

Win32 API (Synchronization)

InterLockedIncrement InterLockedDecrement InterLockedExchange InterLockedExchangeAdd CreateSemaphore ReleaseSemaphore CloseHandle

Win32 API For Synchronization Example

Download Demo Program From K.TOP : Racing Condition Critical Section Mutex Semphore

Delphi & Multi-Thread (1)

TThread Class TThread.Suspend Method TThread.Resume Method TThread.Terminate Method Synchronize() [threadvar]

Delphi & Multi-Thread (2)

Delphi K.TOP

【Delphi】【發表】Multi-Thread 簡報 Demo

範例程式 http://delphi.ktop.com.tw/topic.asp?TOPIC_ID=64439

Multi-thread / Process 的工作分配 [ 利用 Timer 與Semaphore] http://delphi.ktop.com.tw/topic.asp?TOPIC_ID=62971

【問題】多執行緒 ( 均帶無窮迴圈 ) 能同時執行嗎 ? http://delphi.ktop.com.tw/TOPIC.ASP?TOPIC_ID=59940

【Delphi】【問題】WaitForMultipleObjects 的問題..

http://delphi.ktop.com.tw/TOPIC.ASP?TOPIC_ID=50545

Reference

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

敬請指教

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

top related