赞助

Windows 多线程知识点汇总

一、什么叫原子性?

答:一个操作不会被分成两个时间片来执行,不会刚执行到一半,由于时间片到了,CPU就跑去执行其他线程了。在多线程环境中对一个变量进行读写时,我们需要有一种方法能够保证对一个值的操作是原子操作——即不可打断性,一个线程在执行原子操作时,其它线程必须等待它完成之后才能开始执行该原子操作。

二、_beginthreadex()和CreateThread()的区别

转载:http://blog.csdn.net/morewindows/article/details/7421759

转载:http://www.cppblog.com/mzty/archive/2007/07/25/28756.html

 1.CreateThread()函数是Windows提供的API接口,在C/C++语言另有一个创建线程的函数_beginthreadex()

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

 

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()函数的区别。

 1 //_beginthreadex源码整理By MoreWindows( http://blog.csdn.net/MoreWindows )
 2 _MCRTIMP uintptr_t __cdecl _beginthreadex(
 3     void *security,
 4     unsigned stacksize,
 5     unsigned (__CLR_OR_STD_CALL * initialcode) (void *),
 6     void * argument,
 7     unsigned createflag,
 8     unsigned *thrdaddr
 9 )
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     
16     // validation section 检查initialcode是否为NULL
17     _VALIDATE_RETURN(initialcode != NULL, EINVAL, 0);
18 
19     //Initialize FlsGetValue function pointer
20     __set_flsgetvalue();
21     
22     //Allocate and initialize a per-thread data structure for the to-be-created thread.
23     //相当于new一个_tiddata结构,并赋给_ptiddata指针。
24     if ( (ptd = (_ptiddata)_calloc_crt(1, sizeof(struct _tiddata))) == NULL )
25         goto error_return;
26 
27     // Initialize the per-thread data
28     //初始化线程的_tiddata块即CRT数据区域 见注2
29     _initptd(ptd, _getptd()->ptlocinfo);
30     
31     //设置_tiddata结构中的其它数据,这样这块_tiddata块就与线程联系在一起了。
32     ptd->_initaddr = (void *) initialcode; //线程函数地址
33     ptd->_initarg = argument;              //传入的线程参数
34     ptd->_thandle = (uintptr_t)(-1);
35     
36 #if defined (_M_CEE) || defined (MRTDLL)
37     if(!_getdomain(&(ptd->__initDomain))) //见注3
38     {
39         goto error_return;
40     }
41 #endif  // defined (_M_CEE) || defined (MRTDLL)
42     
43     // Make sure non-NULL thrdaddr is passed to CreateThread
44     if ( thrdaddr == NULL )//判断是否需要返回线程ID号
45         thrdaddr = &dummyid;
46 
47     // Create the new thread using the parameters supplied by the caller.
48     //_beginthreadex()最终还是会调用CreateThread()来向系统申请创建线程
49     if ( (thdl = (uintptr_t)CreateThread(
50                     (LPSECURITY_ATTRIBUTES)security,
51                     stacksize,
52                     _threadstartex,
53                     (LPVOID)ptd,
54                     createflag,
55                     (LPDWORD)thrdaddr))
56         == (uintptr_t)0 )
57     {
58         err = GetLastError();
59         goto error_return;
60     }
61 
62     //Good return
63     return(thdl); //线程创建成功,返回新线程的句柄.
64     
65     //Error return
66 error_return:
67     //Either ptd is NULL, or it points to the no-longer-necessary block
68     //calloc-ed for the _tiddata struct which should now be freed up.
69     //回收由_calloc_crt()申请的_tiddata块
70     _free_crt(ptd);
71     // Map the error, if necessary.
72     // Note: this routine returns 0 for failure, just like the Win32
73     // API CreateThread, but _beginthread() returns -1 for failure.
74     //校正错误代号(可以调用GetLastError()得到错误代号)
75     if ( err != 0L )
76         _dosmaperr(err);
77     return( (uintptr_t)0 ); //返回值为NULL的效句柄
78 }

讲解下部分代码:

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

      typedefstruct_tiddata * _ptiddata

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

 

注2._initptd(ptd, _getptd()->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->__initDomain)))中的_getdomain()函数代码可以在thread.c文件中找到,其主要功能是初始化COM环境。

由上面的源代码可知,_beginthreadex()函数在创建新线程时会分配并初始化一个_tiddata块。这个_tiddata块自然是用来存放一些需要线程独享的数据。事实上新线程运行时会首先将_tiddata块与自己进一步关联起来。然后新线程调用标准C运行库函数如strtok()时就会先取得_tiddata块的地址再将需要保护的数据存入_tiddata块中。这样每个线程就只会访问和修改自己的数据而不会去篡改其它线程的数据了。因此,如果在代码中有使用标准C运行库中的函数时,尽量使用_beginthreadex()来代替CreateThread()

三、C/C++ Runtime 多线程函数

1)如果你正在编写C/C++代码,决不应该调用CreateThread。相反,应该使用VisualC++运行期库函数_beginthreadex,推出也应该使用_endthreadex。如果不使用Microsoft的VisualC++编译器,你的编译器供应商有它自己的CreateThred替代函数。不管这个替代函数是什么,你都必须使用。

2)因为_beginthreadex和_endthreadex是CRT线程函数,所以必须注意编译选项runtimelibaray的选择,使用MT或MTD。

3) _beginthreadex函数的参数列表与CreateThread函数的参数列表是相同的,但是参数名和类型并不完全相同。这是因为Microsoft的C/C++运行期库的开发小组认为,C/C++运行期函数不应该对Windows数据类型有任何依赖。_beginthreadex函数也像CreateThread那样,返回新创建的线程的句柄。
下面是关于_beginthreadex的一些要点:
•每个线程均获得由C/C++运行期库的堆栈分配的自己的tiddata内存结构。(tiddata结构位于Mtdll.h文件中的VisualC++源代码中)。

•传递给_beginthreadex的线程函数的地址保存在tiddata内存块中。传递给该函数的参数也保存在该数据块中。

•_beginthreadex确实从内部调用CreateThread,因为这是操作系统了解如何创建新线程的唯一方法。

•当调用CreatetThread时,它被告知通过调用_threadstartex而不是pfnStartAddr来启动执行新线程。还有,传递给线程函数的参数是tiddata结构而不是pvParam的地址。

•如果一切顺利,就会像CreateThread那样返回线程句柄。如果任何操作失败了,便返回NULL。

4) _endthreadex的一些要点:
•C运行期库的_getptd函数内部调用操作系统的TlsGetValue函数,该函数负责检索调用线程的tiddata内存块的地址。

•然后该数据块被释放,而操作系统的ExitThread函数被调用,以便真正撤消该线程。当然,退出代码要正确地设置和传递。

5)虽然也提供了简化版的的_beginthread和_endthread,但是可控制性太差,所以一般不使用。

6)线程handle因为是内核对象,所以需要在最后closehandle。

7)更多的API:HANDLE GetCurrentProcess();HANDLE GetCurrentThread();DWORD GetCurrentProcessId();DWORD GetCurrentThreadId()。DWORD SetThreadIdealProcessor(HANDLE hThread,DWORD dwIdealProcessor);BOOL SetThreadPriority(HANDLE hThread,int nPriority);BOOL SetPriorityClass(GetCurrentProcess(),  IDLE_PRIORITY_CLASS);BOOL GetThreadContext(HANDLE hThread,PCONTEXT pContext);BOOL SwitchToThread();

四、给线程函数传参

1 typedef struct
2 {
3     HWND hwnd;// 窗口句柄
4     ....
5     ....
6 
7     BOOL bKill;//是否退出
8 }PARAMS, *PPARAMS;
1 PARAMS params;//
2 HANDLE Handle;//线程句柄
3 unsigned int m_dwThreadId;//线程ID
4 
5 params.hwnd = this->GetHWND();
6 params.bKill    =  true;
7 
8 Handle = _beginthreadex(NULL,0,ThreadFunction,&params,0,&dwThreadId);

线程函数

 1 UINT WINAPI ThreadFunction(LPVOID pParam)
 2 {
 3     PPARAMS pparams;
 4     
 5     pparams = (PPARAMS) pvoid ;
 6 
 7      ......//具体你的代码
 8            
 9      return 0;       
10 }

 

posted @ 2016-10-11 10:15  车臣  阅读(417)  评论(0编辑  收藏  举报