Вы находитесь на странице: 1из 26

Windows

Windows

Windows..........................................................................3
...................................................................................................................3
, .....................................................................3
, ...........................................................................4
- APC.........7
...........................................................................................................8
Event ().......................................................................................................................8
Mutex (Mutually Exclusive)...................................................................................................11
Semaphore ()............................................................................................................13
Waitable timer ( ).......................................................................................15
lpDueTime..........................................................................................................................16
lPeriod.................................................................................................................................16
pfnCompletionRoutine.......................................................................................................16
lpArgToCompletionRoutine...............................................................................................16
fResume..............................................................................................................................16
............................................................................18
(change notification).......................................................18
(console input)...............................................21
(Job).........................................................................................................................21
(Process)..................................................................................................................21
(thread)........................................................................................................................22
.........................................................................22
.............................................................................................................22
(Interlocked Variable Access)..................................24
............................................................................................................................................25

Windows

Windows
(
) - . Win32
,
, (,
), ,
.
,
, .
, Win32
.
Signaled Not Signaled.
, Not Signaled.
, , ,
- ,
.
, .
, Win32 API.


,
,

,

function WaitForSingleObject(
hHandle: THandle;
//
dwMilliseconds: DWORD
//
): DWORD; stdcall;

hHandle
dwMilliseconds . dwMilliseconds
INFINITE, . dwMilliseconds
0, .
:
WAIT_ABANDONED

, , ,
.

WAIT_OBJECT_0

WAIT_TIMEOUT

.
,

WAIT_FAILED

, hHandle.
, GetLastError

Windows

Action1 ObjectHandle
. , ,
ObjectHandle , CreateProcess.
var
Reason: DWORD;
ErrorCode: DWORD;
Action1.Enabled := FALSE;
try
repeat
Application.ProcessMessages;
Reason := WailForSingleObject(ObjectHandle, 10);
if Reason = WAIT_FAILED then begin
ErrorCode := GetLastError;
raise Exception.CreateFmt(
Wait for object failed with error: %d, [ErrorCode]);
end;
until Reason <> WAIT_TIMEOUT;
finally
Actionl.Enabled := TRUE;
end;

, ,
:
function SignalObjectAndWait(
hObjectToSignal: THandle; //
//
hObjectToWaitOn: THandle; //
dwMilliseconds: DWORD;
//
bAlertable: BOOL
//
//
//
): DWORD; stdcall;

,

,

,

-

WaitForSingleObject.
!

Windows.pas ,
BOOL.
DWORD

hObjectToSignal , (event), .
bAlertable , , ,
-,
. .

,

. :
type
TWOHandleArray = array[0..MAXIMUM_WAIT_OBJECTS - 1] of THandle;
PWOHandleArray = ^TWOHandleArray;
function WaitForMultipleObjects(
nCount: DWORD;
//
lpHandles: PWOHandleArray; //
bWaitAll: BOOL;
// ,

Windows

dwMilliseconds: DWORD
): DWORD; stdcall;

//
//

:

WAIT_OBJECT_0
WAIT_OBJECT_0 + nCount 1

bWaitAll TRUE, ,
.


WAIT_ABANDONED_0
WAIT_ABANDONED_0 + nCount
1

bWaitAll TRUE ,
,
,
.

FALSE ,
WAIT_OBJECT_0,
lpHandles.

FALSE ,
WAIT_ABANDONED_0,
lpHandles, , ,
, .
WAIT_TIMEOUT

WAIT_FAILED

,
, .
var
Handles: array[0..1] of THandle;
Reason: DWORD;
RestIndex: Integer;
...
Handles[0] := OpenMutex(SYNCHRONIZE, FALSE, FirstResource);
Handles[1] := OpenMutex(SYNCHRONIZE, FALSE, SecondResource);
//
Reason := WaitForMultipleObjects(2, @Handles, FALSE, INFINITE);
case Reason of
WAIT_FAILED: RaiseLastWin32Error;
WAIT_OBJECT_0, WAIT_ABANDONED_0:
begin
ModifyFirstResource;
RestIndex := 1;
end;
WAIT_OBJECT_0 + 1, WAIT_ABANDONED_0 + 1:
begin
ModifySecondResource;
RestIndex := 0;
end;
// WAIT_TIMEOUT
end;
//
if WailForSingleObject(Handles[RestIndex],
INFINITE) = WAIT_FAILED then
RaiseLastWin32Error;
// , .
if RestIndex = 0 then

Windows
ModifyFirstResource
else
ModifySecondResource;

, ,
.
.
,
Windows.
( , WAIT_TIMEOUT),
:
function MsgWaitForMultipleObjects(
nCount: DWORD;
//
var pHandles;
//
fWaitAll: BOOL;
// ,
//
dwMilliseconds,
//
dwWakeMask: DWORD // ,
): DWORD; stdcall;

dwWakeMask,
QS_XXX ,
, . ,
QS_KEY WM_KEYUP,
WM_KEYDOWN, WM_SYSKEYUP WM_SYSKEYDOWN, QS_PAINT WM_PAINT. , dwWakeMask
Windows SDK. , ,
,
WAIT_OBJECT_0 + nCount. ,
. .
,
, .
procedure TForm1.Button1Click(Sender: TObject);
var
PI: TProcessInformation;
SI: TStartupInfo;
Reason: DWORD;
Msg: TMsg;
begin
// TStartupInfo
FillChar(SI, SizeOf(SI), 0);
SI.cb := SizeOf(SI);
//
Win32Check(CreateProcess(NIL, 'COMMAND.COM', NIL,
NIL, FALSE, 0, NIL, NIL, SI, PI));
//**************************************************
//
// WaitForSingleObject(PI.hProcess, INFINITE);
// ,
//
//**************************************************
repeat
//
// WM_PAINT
Reason := MsgWaitForMultipleObjects(1, PI.hProcess, FALSE,
INFINITE, QS_PAINT);

Windows
if Reason = WAIT_OBJECT_0 + 1 then begin
// WM_PAINT Windows
// .
//
PeekMessage(Msg, 0, WM_PAINT, WM_PAINT, PM_REMOVE);
//
Update;
end;
// ,
until Reason = WAIT_OBJECT_0;
//
while PeekMessage(Msg, 0, 0, 0, PM_REMOVE) do;
CloseHandle(PI.hProcess);
CloseHandle(PI.hThread)
end;

, ( CreateWindow)
( TForm, DDE, COM) Windows
.
, , ( ,
,
) Windows.

MsgWaitForMultipleObjects

MsgWaitForMultipleObjectsEx .
.

APC
Windows .
(thread) (APC queue).
( ,
QueueUserAPC)
. ,
. , ,
:
function SleepEx(
dwMilliseconds: DWORD;
bAlertable: BOOL

//
//
//
//

): DWORD; stdcall;
function WaitForSingleObjectEx(
hHandle: THandle;
//
dwMilliseconds: DWORD; //
bAlertable: BOOL
// ,
//
//
): DWORD; stdcall;
function WaitForMultipleObjectsEx(
nCount: DWORD;
//
lpHandles: PWOHandleArray;//
bWaitAll: BOOL;
// ,

Windows

dwMilliseconds: DWORD;
bAlertable: BOOL
): DWORD; stdcall;

//
//
//
//
//

function SignalObjectAndWait(
hObjectToSignal: THandle; //
//
hObjectToWaitOn: THandle; //
dwMilliseconds: DWORD;
//
bAlertable: BOOL
//
//
//
): DWORD; stdcall;

,

,

,

function MsgWaitForMultipleObjectsEx(
nCount: DWORD;
//
var pHandles;
//
fWaitAll: BOOL;
// ,
//
dwMilliseconds,
//
dwWakeMask: DWORD // ,
dwFlags: DWORD
//
): DWORD; stdcall;

bAlertable

TRUE
(
dwFlags

MsgWaitForMultipleObjectsEx MWMO_ALERTABLE) ,
APC
,
WAIT_IO_COMPLETION.
, , -.
-
ReadFileEx WriteFileEx, -
.
. , ,
, ,
, -.
APC , ,
-.


Windows,
. ,
,
, . :
Event ()
. Event

Windows

,
ResetEvent


,
.

:
function CreateEvent(
lpEventAttributes: PSecurityAttributes;
bManualReset,
bInitialState: BOOL;
lpName: PChar
): THandle; stdcall;

//
//
//
//
//
//
//

//
// TSecurityAttributes
, Event
(TRUE) (FALSE)
. TRUE
NIL,

TSecurityAttributes , :
TSecurityAttributes = record
nLength: DWORD;

//
//
//
lpSecurityDescriptor: Pointer; //
//
//
bInheritHandle: BOOL;
//
//
end;

,

SizeOf(TSecurityAttributes)
.
Windows 95 98
NIL
,

Windows NT
, lpEventAttributes
NIL.
.
lpName . lpName
Event,
, ,
. bManualReset, bInitialState
lpSecurityDescriptor. , ,
:
hEvent := CreateEvent(NIL, TRUE, FALSE, EventName);
if hEvent = 0 then
RaiseLastWin32Error;
if GetLastError = ERROR_ALREADY_EXISTS then begin
//
end;

,
.

Semaphore, Mutex, Job, Waitable Timer FileMapping. ,
.

Windows

, Event ,
CreateEvent :
function OpenEvent(
dwDesiredAccess: DWORD;
bInheritHandle: BOOL;
lpName: PChar
): THandle; stdcall;

//
//
//
//

, 0, .
dwDesiredAccess :
EVENT_ALL_ACCESS

EVENT_MODIFY_STA
TE
SetEvent ResetEvent
SYNCHRONIZE

Windows NT

.
:
function SetEvent(hEvent: THandle): BOOL; stdcall;


function ResetEvent(hEvent: THandle): BOOL; stdcall;

,
function PulseEvent(hEvent: THandle): BOOL; stdcall

, ,
, .
WinAPI , -.
,
, .
, .
var
Events: array[0..1] of THandle; //
Overlapped: array[0..1] of TOverlapped;
...
//
Events[0] := CreateEvent(NIL, TRUE, FALSE, NIL);
Events[1] := CreateEvent(NIL, TRUE, FALSE, NIL);
// TOverlapped
FillChar(Overlapped, SizeOf(Overlapped), 0);
Overlapped[0].hEvent := Events[0];
Overlapped[1].hEvent := Events[1];
//
WriteFile(hFirstFile, FirstBuffer, SizeOf(FirstBuffer),
FirstFileWritten, @Overlapped[0]);
WriteFile(hSecondFile, SecondBuffer, SizeOf(SecondBuffer),

10

Windows
SecondFileWritten, @Overlapped[1]);
//
WaitForMultipleObjects(2, @Events, TRUE, INFINITE);
//
CloseHandle(Events[0]);
CloseHandle(Events[1]);

, CloseHandle.
Delphi TEvent, Event.
SyncObjs.pas :
type
TWaitResult = (wrSignaled, wrTimeout, wrAbandoned, wrError);
TEvent = class(THandleObject)
public
constructor Create(EventAttributes: PSecurityAttributes;
ManualReset, InitialState: Boolean; const Name: string);
function WaitFor(Timeout: DWORD): TWaitResult;
procedure SetEvent;
procedure ResetEvent;
end;

.
Windows API.
.
type
TSimpleEvent = class(TEvent)
public
constructor Create;
end;

constructor TSimpleEvent.Create;
begin
FHandle := CreateEvent(nil, True, False, nil);
end;

Mutex (Mutually Exclusive)


,
, .
,
, .

. :
function CreateMutex(
lpMutexAttributes: PSecurityAttributes;
bInitialOwner: BOOL;
lpName: PChar
): THandle; stdcall;

//
// TSecurityAttributes
// ,
//
//

11

Windows

, 0.
, .
GetLastError ERROR_ALREDY_EXISTS.
Semaphore, Event, Job, Waitable Timer
FileMapping
, ,
(.. bInitialOwner
FALSE).
,
function OpenMutex(
dwDesiredAccess: DWORD;
bInheritHandle: BOOL;
lpName: PChar
): THandle; stdcall;

//
//
//
//

dwDesiredAccess
MUTEX_ALL_ACCESS

SYNCHRONIZE

Windows NT
ReleaseMutex

, 0, .
,
.
function ReleaseMutex(hMutex: THandle): BOOL; stdcall;

, , ,
,
:
var
Mutex: THandle;
//
Mutex := CreateMutex(NIL, FALSE, UniqueMutexName);
if Mutex = 0 then
RaiseLastWin32Error;
...
//
WaitForSingleObject(Mutex, INFINITE);
try
// , ,
//
// WaitForSingleObject
...
finally
// ,
//
ReleaseMutex(Mutex);
end;
...
//
CloseHandle(Mutex);

12

Windows

, ,
, ,
.
, ,
MsgWaitForSingleObject,
WaitForSingleObject , .
. try ... finally,
,
.
Semaphore ()
, 0
. ,
,
ReleaseSemaphore. 0
, .

, .
:
function CreateSemaphore(
lpSemaphoreAttributes: PSecurityAttributes; //
// TSecurityAttributes
lInitialCount,
//
lMaximumCount: Longint; //
lpName: PChar
//
): THandle; stdcall;

, 0,
.
lMaximumCount , lInitialCount
0 lMaximumCount.
lpName . ,
, .
, ,
lpName NIL.
event, mutex, waitable timer, job, file-mapping.
, , :
function OpenSemaphore(
dwDesiredAccess: DWORD;
bInheritHandle: BOOL;
lpName: PChar
): THandle; stdcall;

//
//
//
//

dwDesiredAccess :
SEMAPHORE_ALL_ACCES
S

13

Windows

SEMAPHORE_MODIFY_ST
ATE


ReleaseSemaphore

SYNCHRONIZE

Windows NT

:
function ReleaseSemaphore(
hSemaphore: THandle;
lReleaseCount: Longint;
lpPreviousCount: Pointer

//
//
//
//
//



32- ,

): BOOL; stdcall;


CreateSemaphore , ReleaseSemaphore FALSE
. lpPreviousCount NIL,
.
,
(, Internet).
,
. ,
,
.
unit LimitedThread;
interface
uses Classes;
type
TLimitedThread = class(TThread)
procedure Execute; override;
end;
implementation
uses Windows;
const
MAX_THREAD_COUNT = 10;
var
Semaphore: THandle;
procedure TLimitedThread.Execute;
begin
// .
// MAX_THREAD_COUNT 0
// .
// .
WaitForSingleObject(Semaphore, INFINITE);
// , ,
//
...

14

Windows

// ,
// .
ReleaseSemaphore(Semaphore, 1, NIL);
end;
initialization
//
Semaphore := CreateSemaphore(NIL, MAX_THREAD_COUNT,
MAX_THREAD_COUNT, NIL);
finalization
//
CloseHandle(Semaphore);
end;

Waitable timer ( )
Windows 95 Windows
98 Windows NT 4.0 .

. :
function CreateWaitableTimer(
lpTimerAttributes: PSecurityAttributes;
bManualReset: BOOL;
lpTimerName: PChar
): THandle; stdcall;

//
//
//
//

//
// TSecurityAttributes
,

bManualReset TRUE,
SetWaitableTimer, FALSE -
.
lpTimerName
,
.
event, semaphore, mutex, job file-mapping.
:
function OpenWaitableTimer(
dwDesiredAccess: DWORD; //
bInheritHandle: BOOL;
//
//
lpTimerName: PChar
//
): THandle; stdcall;

dwDesiredAccess :
TIMER_ALL_ACCESS

TIMER_MODIFY_STATE


SetWaitableTimer CancelWaitableTimer

SYNCHRONIZE

Windows NT

15

Windows

function SetWaitableTimer(
hTimer: THandle;
//
const lpDueTime: TLargeInteger;
//
lPeriod: Longint;
//
pfnCompletionRoutine: TFNTimerAPCRoutine; // -
lpArgToCompletionRoutine: Pointer;// -
fResume: BOOL
// ,
//
): BOOL; stdcall;

.
lpDueTime
. TFileTime
coordinated universal time (UTC), .. .
TFileTime SystemTimeToFileTime.
, ,
.
lPeriod
. lPeriod 0
.
pfnCompletionRoutine
, :
procedure TimerAPCProc(
lpArgToCompletionRoutine: Pointer; //
dwTimerLowValue: DWORD;
// 32
dwTimerHighValue: DWORD; // 32
); stdcall;

, , ,
, ,
. 3 :

lpArgToCompletionRoutine ,
SetWaitableTimer.
,

dwTimerLowValue dwTimerHighValue dwLowDateTime


dwHighDateTime TFileTime. .
UTC ( ).

,
NIL.
lpArgToCompletionRoutine
pfnCompletionRoutine .
fResume
16

Windows

,
(suspended).
fResume TRUE, SetWaitableTimer
, GetLastError
ERROR_NOT_SUPPORTED.
, :
function CancelWaitableTimer(hTimer: THandle): BOOL; stdcall;


-.
CloseHandle
, ,
. ,
, .
,
, Windows.
unit WaitThread;
interface
uses Classes, Windows;
type
TWaitThread = class(TThread)
WaitUntil: TDateTime;
procedure Execute; override;
end;
implementation
uses SysUtils;
procedure TWaitThread.Execute;
var
Timer: THandle;
SystemTime: TSystemTime;
FileTime, LocalFileTime: TFileTime;
begin
Timer := CreateWaitableTimer(NIL, FALSE, NIL);
try
DateTimeToSystemTime(WaitUntil, SystemTime);
SystemTimeToFileTime(SystemTime, LocalFileTime);
LocalFileTimeToFileTime(LocalFileTime, FileTime);
SetWaitableTimer(Timer, TLargeInteger(FileTime), 0,
NIL, NIL, FALSE);
WaitForSingleObject(Timer, INFINITE);
finally
CloseHandle(Timer);
end;
end;
end.

, , :
17

Windows
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
procedure TimerFired(Sender: TObject);
end;
...

implementation
uses WaitThread;
procedure TForm1.Button1Click(Sender: TObject);
var
T: TDateTime;
begin
with TWaitThread.Create(TRUE) do
begin
OnTerminate := TimerFired;
FreeOnTerminate := TRUE;
// 5
WaitUntil := Now + 1 / 24 / 60 / 60 * 5;
Resume;
end;
end;
procedure TForm1.TimerFired(Sender: TObject);
begin
ShowMessage('Timer fired !');
end;


Win32 API ,
. :
(change notification)
Windows .

function FindFirstChangeNotification(
lpPathName: PChar;
// ,
//
bWatchSubtree: BOOL;
//
//
dwNotifyFilter: DWORD //
): THandle; stdcall;

dwNotifyFilter :
FILE_NOTIFY_CHANGE_FILE_NAME
, ..

FILE_NOTIFY_CHANGE_DIR_NAME

18

Windows

, ..

FILE_NOTIFY_CHANGE_ATTRIBUTES

FILE_NOTIFY_CHANGE_SIZE
.
.

FILE_NOTIFY_CHANGE_LAST_WRITE
, .., ,
.
.
FILE_NOTIFY_CHANGE_SECURITY

, ,
. ,
. , :
function FindNextChangeNotification(
hChangeHandle: THandle
): BOOL; stdcall;

, :
function FindCloseChangeNotification(
hChangeHandle: THandle
): BOOL; stdcall;

,
.
TThread. Terminate
, , Execute,
Terminate. WaitForSingleObject
WaitForMultipleObjects, (event),
Terminate.
type
TCheckFolder = class(TThread)
private
FOnChange: TNotifyEvent;
Handles: array[0..1] of THandle;

//
//

procedure DoOnChange;
protected
procedure Execute; override;
public
constructor Create(CreateSuspended: Boolean;
PathToMonitor: String; WaitSubTree: Boolean;
OnChange: TNotifyEvent; NotifyFilter: DWORD);
destructor Destroy; override;
procedure Terminate;
end;

19

Windows

procedure TCheckFolder.DoOnChange;
//
// VCL, ,
// TListBox,
begin
if Assigned(FOnChange) then
FOnChange(Self);
end;
procedure TCheckFolder.Terminate;
begin
inherited; // TThread.Terminate,
// Terminated = TRUE
SetEvent(Handles[1]); //
//
end;
constructor TCheckFolder.Create(CreateSuspended: Boolean;
PathToMonitor: String; WaitSubTree: Boolean;
OnChange: TNotifyEvent; NotifyFilter: DWORD);
var
BoolForWin95: Integer;
begin
//
inherited Create(TRUE);
// Windows 95
// FindFirstChangeNotification. , ,
// :
// - lpPathName - "\"
//

// - bWatchSubtree - TRUE BOOL(1)
if WaitSubTree then
BoolForWin95 := 1
else
BoolForWin95 := 0;
if (Length(PathToMonitor) > 1) and
(PathToMonitor[Length(PathToMonitor)] = \) and
(PathToMonitor[Length(PathToMonitor)-1] <> :) then
Delete(PathToMonitor, Length(PathToMonitor), 1);
Handles[0] := FindFirstChangeNotification(
PChar(PathToMonitor), BOOL(BoolForWin95), NotifyFilter);
Handles[1] := CreateEvent(NIL, TRUE, FALSE, NIL);
FOnChange := OnChange;
// , ,
if not CreateSuspended then
Resume;
end;
destructor TCheckFolder.Destroy;
begin
FindCloseChangeNotification(Handles[0]);
CloseHandle(Handles[1]);
inherited;
end;
procedure TCheckFolder.Execute;

20

Windows
var
Reason: Integer;
Dummy: Integer;
begin
repeat
// ,
//
Reason := WaitForMultipleObjects(2, @Handles, FALSE, INFINITE);
if Reason = WAIT_OBJECT_0 then begin
// ,
//
Synchronize(DoOnChange);
//
FindNextChangeNotification(Handles[0]);
end;
until Terminated;
end;

TThread.Terminate ,
TThread, .. Terminate TThread,
, ,
.
(console input)
, ,
GetStdHandle(STD_INPUT_HANDLE), .
,
, . , ,
WaitForMultipleObjects -
.
(Job)
Job Windows 2000,
.
, ,
.
(Process)
, CreateProcess,
.
, , .
var
PI: TProcessInformation;
SI: TStartupInfo;
...
FillChar(SI, SizeOf(SI), 0);
SI.cb := SizeOf(SI);
Win32Check(CreateProcess(NIL, 'COMMAND.COM', NIL,
NIL, FALSE, 0, NIL, NIL, SI, PI));
//
WaitForSingleObject(PI.hProcess, INFINITE);
CloseHandle(PI.hProcess);
CloseHandle(PI.hThread);

21

Windows

,
. ,
, ,
, MsgWaitForMultipleObjects.
(thread)
,
. .
, , ,
, ,
.



,
. ,
, ,
, .
:
procedure InitializeCriticalSection(
var lpCriticalSection: TRTLCriticalSection
); stdcall;

,
:
procedure EnterCriticalSection(
var lpCriticalSection: TRTLCriticalSection
); stdcall;

,
.
,
.
, ,
EnterCriticalSection .
:
procedure LeaveCriticalSection(
var lpCriticalSection: TRTLCriticalSection
); stdcall;


EnterCriticalSection. ,
, .
, , ,
.
.
:
function TryEnterCriticalSection(
var lpCriticalSection: TRTLCriticalSection

22

Windows
): BOOL; stdcall;

, .
FALSE, TRUE.
,
:
procedure DeleteCriticalSection(
var lpCriticalSection: TRTLCriticalSection
); stdcall;

,
. BytesSummary TimeSummary
.
.
.
var
//
CriticalSection: TRTLCriticalSection;
BytesSummary: Cardinal;
TimeSummary: TDateTime;
AverageSpeed: Float;
...
//
InitializeCriticalSection(CriticalSection);
BytesSummary := 0;
TimeSummary := 0;
AverageSpeed := 0;
// Execute , .
repeat
BytesRead := ReadDataBlockFromNetwork;
EnterCriticalSection(CriticalSection);
try
BytesSummary := BytesSummary + BytesRead;
TimeSummary := TimeSummary + (Now - ThreadStartTime);
if TimeSummary > 0 then
AverageSpeed := BytesSummary / (TimeSummary/24/60/60);
finally
LeaveCriticalSection(CriticalSection)
end;
until LoadComplete;
//
DeleteCriticalSection(CriticalSection);

Delphi , .
SyncObjs.pas
type
TCriticalSection = class(TSynchroObject)
public
constructor Create;
destructor Destroy; override;
procedure Acquire; override;
procedure Release; override;

23

Windows
procedure Enter;
procedure Leave;
end;

Enter Leave Acquire Release


.
procedure TCriticalSection.Enter;
begin
Acquire;
end;
procedure TCriticalSection.Leave;
begin
Release;
end;

(Interlocked Variable Access)



32- . WinAPI
, (
) . , ,
32- . Delphi
, (record),
(packed)
{$A+}.
.
type
TPackedRecord = packed record
A: Byte;
B: Integer;
end;
// TPackedRecord.B InterlockedXXX
TNotPackedRecord = record
A: Byte;
B: Integer;
end;
{$A-}
var
A1: TNotPackedRecord;
// A1.B InterlockedXXX
I: Integer
// I InterlockedXXX, ..
// Delphi
// $A
{$A+}
var
A2: TNotPackedRecord;
// A2.B InterlockedXXX

24

Windows
function InterlockedIncrement(
var Addend: Integer
): Integer; stdcall;

Addend 1.
:
Windows 98, Windows NT 4.0
Addend
Windows 95, Windows NT 3.51
Addend < 0 ,
Addend
Addend = 0 0
Addend > 0 ,
Addend.
function InterlockedDecrement(
var Addend: Integer
): Integer; stdcall;

Addend 1.
InterlockedIncrement.
function InterlockedExchange(
var Target: Integer;
Value: Integer
): Integer; stdcall;

Target Value
Target
Windows 98 Windows NT 4.0 .
function InterlockedCompareExchange(
var Destination: Pointer;
Exchange: Pointer;
Comperand: Pointer
): Pointer; stdcall;

Destination Comperand. ,
Exchange Destination. Destination.
function InterlockedExchangeAdd(
Addend: PLongint;
Value: Longint
): Longint; stdcall;

, Addend Value
Addend.

Win32

.
,

,
,
,
. :

25

Windows

,
, .

(event) -
.

32-
,
.

Win32
.
.

( , CoInitialize DDE)
.

. MsgWaitForXXX

26

Вам также может понравиться