---
title: "【转】windows多线程CreateThread与_beginthreadex本质区别"
author: "qimo601"
date: "2012-11-15T14:40:50Z"
tags: []
categories: ["编程语言"]
slug: "windows-createthread-beginthreadex"
draft: false
lastmod: "2012-11-15T14:40:50Z"
id: 1726262
url: "https://qimo601.iteye.com/blog/1726262"
---

本文将带领你与多线程作第一次亲密接触，并深入分析CreateThread与\_beginthreadex的本质区别，相信阅读本文后你能轻松的使用多线程并能流畅准确的回答CreateThread与\_beginthreadex到底有什么区别，在实际的编程中到底应该使用CreateThread还是\_beginthreadex？

 

   使用多线程其实是非常容易的，下面这个程序的主线程会创建了一个子线程并等待其运行完毕，子线程就输出它的线程ID号然后输出一句经典名言——Hello World。整个程序的代码非常简短，只有区区几行。

**\[cpp]** [view plain](http://blog.csdn.net/morewindows/article/details/7421759 "view plain")[copy](http://blog.csdn.net/morewindows/article/details/7421759 "copy")

01. //最简单的创建多线程实例
02. #include &lt;stdio.h&gt;
03. #include &lt;windows.h&gt;
04. //子线程函数
05. DWORD WINAPI ThreadFun(LPVOID pM)
06. {
07. printf("子线程的线程ID号为：%d\\n子线程输出Hello World\\n", GetCurrentThreadId());
08. return 0;
09. }
10. //主函数，所谓主函数其实就是主线程执行的函数。
11. int main()
12. {
13. printf("     最简单的创建多线程实例\\n");
14. printf(" -- by MoreWindows( http://blog.csdn.net/MoreWindows ) --\\n\\n");
15. HANDLE handle = CreateThread(NULL, 0, ThreadFun, NULL, 0, NULL);
16. WaitForSingleObject(handle, INFINITE);
17. return 0;
18. }

运行结果如下所示：

 

![](http://my.csdn.net/uploads/201204/02/1333353173_5312.PNG)

下面来细讲下代码中的一些函数

第一个 CreateThread

函数功能：创建线程

函数原型：

HANDLEWINAPICreateThread(

  LPSECURITY\_ATTRIBUTESlpThreadAttributes,

  SIZE\_TdwStackSize,

  LPTHREAD\_START\_ROUTINElpStartAddress,

  LPVOIDlpParameter,

  DWORDdwCreationFlags,

  LPDWORDlpThreadId

);

函数说明：

第一个参数表示线程内核对象的安全属性，一般传入NULL表示使用默认设置。

第二个参数表示线程栈空间大小。传入0表示使用默认大小（1MB）。

第三个参数表示新线程所执行的线程函数地址，多个线程可以使用同一个函数地址。

第四个参数是传给线程函数的参数。

第五个参数指定额外的标志来控制线程的创建，为0表示线程创建之后立即就可以进行调度，如果为CREATE\_SUSPENDED则表示线程创建后暂停运行，这样它就无法调度，直到调用ResumeThread()。

第六个参数将返回线程的ID号，传入NULL表示不需要返回该线程ID号。

函数返回值：

成功返回新线程的句柄，失败返回NULL。 

 

第二个 WaitForSingleObject

函数功能：等待函数 – 使线程进入等待状态，直到指定的内核对象被触发。

函数原形：

DWORDWINAPIWaitForSingleObject(

  HANDLEhHandle,

  DWORDdwMilliseconds

);

函数说明：

第一个参数为要等待的内核对象。

第二个参数为最长等待的时间，以毫秒为单位，如传入5000就表示5秒，传入0就立即返回，传入INFINITE表示无限等待。

因为线程的句柄在线程运行时是未触发的，线程结束运行，句柄处于触发状态。所以可以用WaitForSingleObject()来等待一个线程结束运行。

函数返回值：

在指定的时间内对象被触发，函数返回WAIT\_OBJECT\_0。超过最长等待时间对象仍未被触发返回WAIT\_TIMEOUT。传入参数有错误将返回WAIT\_FAILED

 

CreateThread()函数是Windows提供的API接口，在C/C++语言另有一个创建线程的函数\_beginthreadex()，在很多书上（包括《Windows核心编程》）提到过尽量使用\_beginthreadex()来代替使用CreateThread()，这是为什么了？下面就来探索与发现它们的区别吧。

 

       首先要从标准C运行库与多线程的矛盾说起，标准C运行库在1970年被实现了，由于当时没任何一个操作系统提供对多线程的支持。因此编写标准C运行库的程序员根本没考虑多线程程序使用标准C运行库的情况。比如标准C运行库的全局变量errno。很多运行库中的函数在出错时会将错误代号赋值给这个全局变量，这样可以方便调试。但如果有这样的一个代码片段：

**\[cpp]** [view plain](http://blog.csdn.net/morewindows/article/details/7421759 "view plain")[copy](http://blog.csdn.net/morewindows/article/details/7421759 "copy")

1. if (system("notepad.exe readme.txt") == -1)
2. {
3. switch(errno)
4. {
5. ...//错误处理代码
6. }
7. }

假设某个线程A在执行上面的代码，该线程在调用system()之后且尚未调用switch()语句时另外一个线程B启动了，这个线程B也调用了标准C运行库的函数，不幸的是这个函数执行出错了并将错误代号写入全局变量errno中。这样线程A一旦开始执行switch()语句时，它将访问一个被B线程改动了的errno。这种情况必须要加以避免！因为不单单是这一个变量会出问题，其它像strerror()、strtok()、tmpnam()、gmtime()、asctime()等函数也会遇到这种由多个线程访问修改导致的数据覆盖问题。

 

为了解决这个问题，Windows操作系统提供了这样的一种解决方案——每个线程都将拥有自己专用的一块内存区域来供标准C运行库中所有有需要的函数使用。而且这块内存区域的创建就是由C/C++运行库函数\_beginthreadex()来负责的。下面列出\_beginthreadex()函数的源代码（我在这份代码中增加了一些注释）以便读者更好的理解\_beginthreadex()函数与CreateThread()函数的区别。

**\[cpp]** [view plain](http://blog.csdn.net/morewindows/article/details/7421759 "view plain")[copy](http://blog.csdn.net/morewindows/article/details/7421759 "copy")

01. //\_beginthreadex源码整理By MoreWindows( http://blog.csdn.net/MoreWindows )
02. \_MCRTIMP uintptr\_t \_\_cdecl \_beginthreadex(
03. void \*security,
04. unsigned stacksize,
05. unsigned (\_\_CLR\_OR\_STD\_CALL * initialcode) (void \*),
06. void * argument,
07. unsigned createflag,
08. unsigned \*thrdaddr
09. )
10. {
11. \_ptiddata ptd;          //pointer to per-thread data 见注1
12. uintptr\_t thdl;         //thread handle 线程句柄
13. unsigned long err = 0L; //Return from GetLastError()
14. unsigned dummyid;    //dummy returned thread ID 线程ID号
15. // validation section 检查initialcode是否为NULL
16. \_VALIDATE\_RETURN(initialcode != NULL, EINVAL, 0);
17. //Initialize FlsGetValue function pointer
18. \_\_set\_flsgetvalue();
19. //Allocate and initialize a per-thread data structure for the to-be-created thread.
20. //相当于new一个\_tiddata结构，并赋给\_ptiddata指针。
21. if ( (ptd = (\_ptiddata)\_calloc\_crt(1, sizeof(struct \_tiddata))) == NULL )
22. goto error\_return;
23. // Initialize the per-thread data
24. //初始化线程的\_tiddata块即CRT数据区域 见注2
25. \_initptd(ptd, \_getptd()-&gt;ptlocinfo);
26. //设置\_tiddata结构中的其它数据，这样这块\_tiddata块就与线程联系在一起了。
27. ptd-&gt;\_initaddr = (void \*) initialcode; //线程函数地址
28. ptd-&gt;\_initarg = argument;              //传入的线程参数
29. ptd-&gt;\_thandle = (uintptr\_t)(-1);
30. #if defined (\_M\_CEE) || defined (MRTDLL)
31. if(!\_getdomain(&(ptd-&gt;\_\_initDomain))) //见注3
32. {
33. goto error\_return;
34. }
35. #endif  // defined (\_M\_CEE) || defined (MRTDLL)
36. // Make sure non-NULL thrdaddr is passed to CreateThread
37. if ( thrdaddr == NULL )//判断是否需要返回线程ID号
38. thrdaddr = &dummyid;
39. // Create the new thread using the parameters supplied by the caller.
40. //\_beginthreadex()最终还是会调用CreateThread()来向系统申请创建线程
41. if ( (thdl = (uintptr\_t)CreateThread(
42. (LPSECURITY\_ATTRIBUTES)security,
43. stacksize,
44. \_threadstartex,
45. (LPVOID)ptd,
46. createflag,
47. (LPDWORD)thrdaddr))
48. == (uintptr\_t)0 )
49. {
50. err = GetLastError();
51. goto error\_return;
52. }
53. //Good return
54. return(thdl); //线程创建成功,返回新线程的句柄.
55. //Error return
56. error\_return:
57. //Either ptd is NULL, or it points to the no-longer-necessary block
58. //calloc-ed for the \_tiddata struct which should now be freed up.
59. //回收由\_calloc\_crt()申请的\_tiddata块
60. \_free\_crt(ptd);
61. // Map the error, if necessary.
62. // Note: this routine returns 0 for failure, just like the Win32
63. // API CreateThread, but \_beginthread() returns -1 for failure.
64. //校正错误代号(可以调用GetLastError()得到错误代号)
65. if ( err != 0L )
66. \_dosmaperr(err);
67. return( (uintptr\_t)0 ); //返回值为NULL的效句柄
68. }

讲解下部分代码：

注1．\_ptiddataptd;中的\_ptiddata是个结构体指针。在mtdll.h文件被定义：

      typedefstruct\_tiddata * \_ptiddata

微软对它的注释为Structure for each thread's data。这是一个非常大的结构体，有很多成员。本文由于篇幅所限就不列出来了。

 

注2．\_initptd(ptd, \_getptd()-&gt;ptlocinfo);微软对这一句代码中的getptd()的说明为：

      /* return address of per-thread CRT data \*/

      \_ptiddata \_\_cdecl\_getptd(void);

对\_initptd()说明如下：

      /* initialize a per-thread CRT data block \*/

      void\_\_cdecl\_initptd(\_Inout_ \_ptiddata \_Ptd,\_In\_opt_ pthreadlocinfo \_Locale);

注释中的CRT （C Runtime Library）即标准C运行库。

 

注3．if(!\_getdomain(&(ptd-&gt;\_\_initDomain)))中的\_getdomain()函数代码可以在thread.c文件中找到，其主要功能是初始化COM环境。

 

由上面的源代码可知，\_beginthreadex()函数在创建新线程时会分配并初始化一个\_tiddata块。这个\_tiddata块自然是用来存放一些需要线程独享的数据。事实上新线程运行时会首先将\_tiddata块与自己进一步关联起来。然后新线程调用标准C运行库函数如strtok()时就会先取得\_tiddata块的地址再将需要保护的数据存入\_tiddata块中。这样每个线程就只会访问和修改自己的数据而不会去篡改其它线程的数据了。因此，**如果在代码中有使用标准C运行库中的函数时，尽量使用\_beginthreadex()来代替CreateThread()。**相信阅读到这里时，你会对这句简短的话有个非常深刻的印象，如果有面试官问起，你也可以流畅准确的回答了^\_^。

 

接下来，类似于上面的程序用CreateThread()创建输出“Hello World”的子线程，下面使用\_beginthreadex()来创建多个子线程：

**\[cpp]** [view plain](http://blog.csdn.net/morewindows/article/details/7421759 "view plain")[copy](http://blog.csdn.net/morewindows/article/details/7421759 "copy")

01. //创建多子个线程实例
02. #include &lt;stdio.h&gt;
03. #include &lt;process.h&gt;
04. #include &lt;windows.h&gt;
05. //子线程函数
06. unsigned int \_\_stdcall ThreadFun(PVOID pM)
07. {
08. printf("线程ID号为%4d的子线程说：Hello World\\n", GetCurrentThreadId());
09. return 0;
10. }
11. //主函数，所谓主函数其实就是主线程执行的函数。
12. int main()
13. {
14. printf("     创建多个子线程实例 \\n");
15. printf(" -- by MoreWindows( http://blog.csdn.net/MoreWindows ) --\\n\\n");
16. const int THREAD\_NUM = 5;
17. HANDLE handle\[THREAD\_NUM];
18. for (int i = 0; i &lt; THREAD\_NUM; i++)
19. handle\[i] = (HANDLE)\_beginthreadex(NULL, 0, ThreadFun, NULL, 0, NULL);
20. WaitForMultipleObjects(THREAD\_NUM, handle, TRUE, INFINITE);
21. return 0;
22. }

运行结果如下：

![](http://my.csdn.net/uploads/201204/02/1333353199_1173.PNG)

图中每个子线程说的都是同一句话，不太好看。能不能来一个**线程报数**功能，即第一个子线程输出1，第二个子线程输出2，第三个子线程输出3，……。要实现这个功能似乎非常简单——每个子线程对一个全局变量进行递增并输出就可以了。代码如下：

**\[cpp]** [view plain](http://blog.csdn.net/morewindows/article/details/7421759 "view plain")[copy](http://blog.csdn.net/morewindows/article/details/7421759 "copy")

01. //子线程报数
02. #include &lt;stdio.h&gt;
03. #include &lt;process.h&gt;
04. #include &lt;windows.h&gt;
05. int g\_nCount;
06. //子线程函数
07. unsigned int \_\_stdcall ThreadFun(PVOID pM)
08. {
09. g\_nCount++;
10. printf("线程ID号为%4d的子线程报数%d\\n", GetCurrentThreadId(), g\_nCount);
11. return 0;
12. }
13. //主函数，所谓主函数其实就是主线程执行的函数。
14. int main()
15. {
16. printf("     子线程报数 \\n");
17. printf(" -- by MoreWindows( http://blog.csdn.net/MoreWindows ) --\\n\\n");
18. const int THREAD\_NUM = 10;
19. HANDLE handle\[THREAD\_NUM];
20. g\_nCount = 0;
21. for (int i = 0; i &lt; THREAD\_NUM; i++)
22. handle\[i] = (HANDLE)\_beginthreadex(NULL, 0, ThreadFun, NULL, 0, NULL);
23. WaitForMultipleObjects(THREAD\_NUM, handle, TRUE, INFINITE);
24. return 0;
25. }

对一次运行结果截图如下：

![](http://my.csdn.net/uploads/201204/02/1333353214_7235.PNG)

 

显示结果从1数到10，看起来好象没有问题。

       答案是不对的，虽然这种做法在逻辑上是正确的，但在多线程环境下这样做是会产生严重的问题，下一篇《[秒杀多线程第三篇 原子操作 Interlocked系列函数](http://blog.csdn.net/morewindows/article/details/7429155)》将为你演示错误的结果（可能非常出人意料）并解释产生这个结果的详细原因。

 

转载请标明出处，原文地址：[http://blog.csdn.net/morewindows/article/details/7421759](http://blog.csdn.net/morewindows/article/details/7421759)

如果觉得本文对您有帮助，请点击‘顶’支持一下，您的支持是我写作最大的动力，谢谢。