2022年3月5日

多线程编程(2) – 一切从 CreateThread 开始

产生一个线程

产生一个线程,是以 CreateThread() 作为一切行动的开始。此函数的原型如下:

function CreateThread(
  lpThreadAttributes: Pointer;  // 安全设置,nil 表示缺省值
  dwStackSize: SIZE_T;  // 堆栈大小,0 表示缺省大小
  lpStartAddress: TFNThreadStartRoutine; // 入口函数,新线程开始的地址,函数指针
  lpParameter: Pointer;   // 函数参数,传递到上面的函数去
  dwCreationFlags: DWORD;                // 启动选项,默认是立即开始执行
  var lpThreadId: DWORD                  // 新线程的 ID 将会传回这里
): THandle; stdcall;                     // 返回值,如果成功返回一个 handle,代表新线程

TThread.Create 先调用了 BeginThread,如下代码片段:

{$IF Defined(MSWINDOWS)}
constructor TThread.Create(CreateSuspended: Boolean; ReservedStackSize: NativeUInt);
begin
  inherited Create;
  FSuspended := not FExternalThread;
  FCreateSuspended := CreateSuspended and not FExternalThread;
  if not FExternalThread then
  begin
    if ReservedStackSize <> 0 then
      FHandle := BeginThread(nil, ReservedStackSize, @ThreadProc, Pointer(Self), CREATE_SUSPENDED or STACK_SIZE_PARAM_IS_A_RESERVATION, FThreadID)
    else
      FHandle := BeginThread(nil, 0, @ThreadProc, Pointer(Self), CREATE_SUSPENDED, FThreadID);
    if FHandle = 0 then
      raise EThread.CreateResFmt(@SThreadCreateError, [SysErrorMessage(GetLastError)]);
  end else
  begin
    FHandle := Winapi.Windows.GetCurrentThread;
    FThreadId := GetCurrentThreadId;
  end;
end;

BeginThread 又调用了 CreateThread,如下代码节选:

{$IFDEF MSWINDOWS}
function BeginThread(SecurityAttributes: Pointer; StackSize: LongWord;
  ThreadFunc: TThreadFunc; Parameter: Pointer; CreationFlags: LongWord;
  var ThreadId: TThreadID): THandle;
var
  P: PThreadRec;
begin
  if Assigned(SystemThreadFuncProc) then
    P := PThreadRec(SystemThreadFuncProc(ThreadFunc, Parameter))
  else
  begin
    New(P);
    P.Func := ThreadFunc;
    P.Parameter := Parameter;
  end;

  IsMultiThread := TRUE;

  Result := CreateThread(SecurityAttributes, StackSize, @ThreadWrapper, P,
    CreationFlags, ThreadID);

  { P variable is supposed to be freed by the ThreadWrapper routine.
    If the call to CreateThread fails, then ThreadWrapper will not be called
    and P will not get freed. Check for failure now and free P if required.
  }
  if Result = 0 then
    Dispose(P);
end;

结束一个线程

可以靠线程函数的结束而结束线程,有时候还需要更强制的手法结束一个线程,使用 ExitThread()

procedure ExitThread(dwExitCode: DWORD); stdcall;

只有一个 dwExitCode 参数,指定此线程的结束代码。

posted @ 2022-03-05 10:38 pchmonster 阅读(285) 评论(0) 推荐(0) 编辑

《Accelerated C++》学习笔记(1)——学习资料

摘要: 最近找到一本C++的书籍,英文名字《Accelerated C++: Practical Programming by Example》,作者 Andrew Koenig, Barbara Moo。 国内也有中文版图书,但是出版日期有点久远(2012年7月第一版),书的价格有点高,可以从网络找PDF 阅读全文

posted @ 2022-03-05 10:29 pchmonster 阅读(1096) 评论(0) 推荐(1) 编辑

2022年3月3日

Delphi 11 保姆级安装教程

摘要: 新版代号为 "Alexandria" 新版本引入了许多重要的新特性和增强功能,官网首页写着一下大字介绍: The Delphi 11 Release Supports High-DPI and 4k+ screens, provisioning for Windows 11, Android 30 阅读全文

posted @ 2022-03-03 15:51 pchmonster 阅读(15203) 评论(1) 推荐(1) 编辑

2022年2月24日

多线程编程(1) - 先入门

摘要: 先编写一个循环 50000 次的程序,每次在程序界面左上方(10,10)的位置输出数字,代码如下: 解决卡死方法1(Application.ProcessMessages) 解决卡死方法2(调用 API) CreateThread 要使用的函数是系统级的,不能是某个类的方法,必须有严格的格式(参数、 阅读全文

posted @ 2022-02-24 23:03 pchmonster 阅读(583) 评论(0) 推荐(1) 编辑

2020年12月5日

(5)名称空间 namespace 和 using 声明

摘要: 名称空间是 C++ 中的一种机制,防止在程序中将相同的名称用于不同的事情时可能发生的问题,将一组给定的名称与一种姓(family name)关联起来,这种姓就是名称空间名称。 所有的标准库工具都定义在一个名称为 std 的名称空间内,名称 cout 和 endl 是在标准库中定义的,他们的全名是 s 阅读全文

posted @ 2020-12-05 21:38 pchmonster 阅读(182) 评论(0) 推荐(0) 编辑

2020年12月4日

(4)#include 指令

摘要: #include 是几个预处理指令中的一个,预处理指令都以 # 字符开头。 例如代码: #include 指令,在编译之前,在此程序源文件中插入文件 iostream 的内容,该文件位于尖括号中。 iostream 文件称之为头文件,因为经常出现在程序文件的开头。 iostream 头文件是标准 C 阅读全文

posted @ 2020-12-04 23:03 pchmonster 阅读(980) 评论(0) 推荐(0) 编辑

2020年12月3日

(3)注释

摘要: 注释(comment)是所有程序的一个重要部分,但它们不是可执行代码,只是增强程序的可读性。 注释并不会增加可执行程序的大小,编译器将忽略所有注释。 // 单行注释 只包含这一行中 // 右边的内容。 /* */ 成对注释 从 C 语言继承过来的,包含在 /* 和 */ 之间的所有内容,可以跨越多行 阅读全文

posted @ 2020-12-03 23:41 pchmonster 阅读(137) 评论(0) 推荐(0) 编辑

2020年12月2日

(2)简单的程序

摘要: C++由一个或多个函数组成。 由 Visual C++ 中 Application Wizard 生成的 Win32 控制台程序具有一个名称为 _tmain 的主函数。 wmain 和 _tmain 是 Microsoft 专有的,符合 C++ 标准的主函数名称是 main。 简单的程序 通过简单的 阅读全文

posted @ 2020-12-02 23:10 pchmonster 阅读(207) 评论(0) 推荐(0) 编辑

2020年11月27日

(1)Hello World

摘要: 语出《论语·卫灵公》:子贡问为仁。子曰:“工欲善其事,必先利其器。居是邦也,事其大夫之贤者,友其士之仁者。” 2020年11月终于下定决心开始 Visual C++ 的学习了,首先要学习 C++,但不考虑任何 Windows 编程事项。在熟悉了 C++ 之后,再进行学习如何开发 Windows 应用 阅读全文

posted @ 2020-11-27 10:37 pchmonster 阅读(221) 评论(0) 推荐(0) 编辑

2019年12月15日

ListView 实现进度条显示

摘要: 代码参考互联网,本人在Win10 + Delphi 10.3.2 社区版中测试通过,现将测试通过的代码分享如下: unit Unit1;interfaceuses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.... 阅读全文

posted @ 2019-12-15 19:22 pchmonster 阅读(1235) 评论(0) 推荐(1) 编辑

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示