⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unitinterlocked.pas

📁 Delphi Win32核心API参考光盘源码 本书包含了常用的Windows API函数
💻 PAS
字号:
unit UnitInterlocked;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Label1: TLabel;
    Label2: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
  private
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  ThreadHandle: THandle;   // holds a thread handle
  ThreadHandle1: THandle;  // holds a thread handle
  MultiVar: Integer;       // the incrementing variable

implementation

{$R *.DFM}

Function ThreadFunc(Info: Pointer): Integer; stdcall;
var
  Count: Integer;         // general loop variable
begin
  {increment the variable by 10}
  for Count := 1 to 10 do
  Begin
    InterlockedIncrement(MultiVar);

    {slow it down a bit to insure that multiple threads will be
     accessing the interlocked variable simultaneously}
    Sleep(1);
  end;

  {exit the thread}
  ExitThread(4);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  ThreadId, ThreadId1: DWORD;   // holds the thread identifiers
begin
  {launch a thread, incrementing the variable by 10}
  ThreadHandle := CreateThread(nil, 0, @ThreadFunc, Nil, 0, ThreadId);

  {increment the variable again by 1}
  InterlockedIncrement(MultiVar);

  {increment the variable by 10 again.  thanks to the InterlockedIncrement
   function, the variable will be exactly equal to 21, even though
   multiple threads have been incrementing it simultaneously}
  ThreadHandle1 := CreateThread(nil, 0, @ThreadFunc, Nil, 0, ThreadId1);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  {show the variable}
  ShowMessage(IntToStr(MultiVar));
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  RtnValue: Integer;     // holds the return value from InterlockedDecrement
begin
  {increment the variable}
  RtnValue := InterlockedDecrement(MultiVar);

  {display the return value}
  Label2.Caption := IntToStr(RtnValue);
end;

procedure TForm1.Button4Click(Sender: TObject);
var
  RtnValue: Integer;    // holds the return value from InterlockedExchange
begin
  {exchange the current variable's value with 50}
  RtnValue := InterlockedExchange(MultiVar, 50);

  {display the return value}
  Label2.Caption := IntToStr(RtnValue);
end;

procedure TForm1.Button5Click(Sender: TObject);
var
  RtnValue: Integer;    // holds the return value from InterlockedIncrement
begin
  {increment the variable}
  RtnValue := InterlockedIncrement(MultiVar);

  {display the return value}
  Label2.Caption := IntToStr(RtnValue);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  {initialize the variable}
  Multivar := 0;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -