多線程編程(7) - 從 CreateThread 說起[續(xù)五]
function CreateThread(
lpThreadAttributes: Pointer; {安全設(shè)置}
dwStackSize: DWORD;
lpStartAddress: TFNThreadStartRoutine;
lpParameter: Pointer;
dwCreationFlags: DWORD;
var lpThreadId: DWORD
): THandle; stdcall;
CreateThread 的第一個參數(shù) lpThreadAttributes 是指向 TSecurityAttributes 結(jié)構(gòu)的指針, 一般都是置為 nil, 這表示沒有訪問限制; 該結(jié)構(gòu)的定義是:
//TSecurityAttributes(又名: SECURITY_ATTRIBUTES、_SECURITY_ATTRIBUTES)
_SECURITY_ATTRIBUTES = record
nLength: DWORD; {結(jié)構(gòu)大小}
lpSecurityDescriptor: Pointer; {默認(rèn) nil; 這是另一個結(jié)構(gòu) TSecurityDescriptor 的指針}
bInheritHandle: BOOL; {默認(rèn) False, 表示不可繼承}
end;
//TSecurityDescriptor(又名: SECURITY_DESCRIPTOR、_SECURITY_DESCRIPTOR)
_SECURITY_DESCRIPTOR = record
Revision: Byte;
Sbz1: Byte;
Control: SECURITY_DESCRIPTOR_CONTROL;
Owner: PSID;
Group: PSID;
Sacl: PACL;
Dacl: PACL;
end;
夠復(fù)雜的, 但我們在多線程編程時不需要去設(shè)置它們, 大都是使用默認(rèn)設(shè)置(也就是賦值為 nil).
我覺得有必要在此刻了解的是: 建立系統(tǒng)內(nèi)核對象時一般都有這個屬性(TSecurityAttributes);
在接下來多線程的課題中要使用一些內(nèi)核對象, 不如先盤點(diǎn)一下, 到時碰到這個屬性時給個 nil 即可, 不必再費(fèi)神.
{建立事件}
function CreateEvent(
lpEventAttributes: PSecurityAttributes; {!}
bManualReset: BOOL;
bInitialState: BOOL;
lpName: PWideChar
): THandle; stdcall;
{建立互斥}
function CreateMutex(
lpMutexAttributes: PSecurityAttributes; {!}
bInitialOwner: BOOL;
lpName: PWideChar
): THandle; stdcall;
{建立信號}
function CreateSemaphore(
lpSemaphoreAttributes: PSecurityAttributes; {!}
lInitialCount: Longint;
lMaximumCount: Longint;
lpName: PWideChar
): THandle; stdcall;
{建立等待計時器}
function CreateWaitableTimer(
lpTimerAttributes: PSecurityAttributes; {!}
bManualReset: BOOL;
lpTimerName: PWideChar
): THandle; stdcall;
上面的四個系統(tǒng)內(nèi)核對象(事件、互斥、信號、計時器)都是線程同步的手段, 從這也能看出處理線程同步的復(fù)雜性; 不過這還不是全部, Windows Vista 開始又增加了 Condition variables(條件變量)、Slim Reader-Writer Locks(讀寫鎖)等同步手段.
不過最簡單、最輕便(速度最快)的同步手段還是 CriticalSection(臨界區(qū)), 但它不屬于系統(tǒng)內(nèi)核對象, 當(dāng)然也就沒有句柄、沒有 TSecurityAttributes 這個安全屬性, 這也導(dǎo)致它不能跨進(jìn)程使用; 不過寫多線程時一般不用跨進(jìn)程啊, 所以 CriticalSection 應(yīng)該是最常用的同步手段.
下次接上, 開始學(xué)習(xí)多線程的同步了.