2019独角兽企业重金招聘Python工程师标准>>>

Win32多线程的创建方法主要有:

  1. CreateThread()

  2. _beginthread()&&_beginthreadex()

  3. AfxBeginThread()

  4. CWinThread类

一、简介

CreateThread: Win32提供的创建线程的最基础的API,用于在主线程上创建一个线程。返回一个HANDLE句柄(内核对象)。在内核对象使用完毕后,一般需要关闭,使用CloseHandle()函数。

_beginthread()&&_beginthreadex():

在MSDN中可以看到一句很重要的提示,内容为“For an executable file linked with Libcmt.lib, do not call the Win32 ExitThread API; this prevents the run-time system from reclaiming allocated resources. _endthread and _endthreadex reclaim allocated thread resources and then call ExitThread.”,简单翻译就是说,对于链接Libcmt.lib的可执行程序,不要使用Win32的线程退出函数(ExitThread),这会阻止运行时系统回收分配的资源,应该使用_endthread,它能回收分配的线程资源然后调用ExitThread。这个问题看似没有提到CreateThread(),但是其实有关,这就是经常看到有些资料上坚决的说到”不要使用CreateThread创建线程,否则会内存泄漏“的来源了。

更详细的介绍见http://wenku.baidu.com/view/adede4ec4afe04a1b071dea4.html

也就是说:尽量用_beginthread()而不是CreateThread

‍‍Windows核心编程上如此说:‍‍

These two functions were originally created to do the work of the new _beginthreadex and _endthreadex functions, respectively. However, as you can see, the _beginthread function has fewer parameters and is therefore more limited than the full-featured _beginthreadex function. For example, if you use _beginthread, you cannot create the new thread with security attributes, you cannot create the thread suspended, and you cannot obtain the thread's ID value. The _endthread function has a similar story: it takes no parameters, which means that the thread's exit code is hardcoded to 0.

_endthread还有个问题:

DWORD dwExitCode;
HANDLE hThread = _beginthread(...);
GetExitCodeThread(hThread, &dwExitCode);
CloseHandle(hThread);

The newly created thread might execute, return, and terminate before the first thread can call GetExitCodeThread. If this happens, the value in hThread will be invalid because _endthread has closed the new thread's handle. Needless to say, the call to CloseHandle will also fail for the same reason.

简单翻译为: 在调用GetExitCodeThread之前,可能新创建的线程已经执行,返回并终止了,这个时候,hThread将无效,_endthread已经关掉了线程句柄。

_beginthreadex > _beginthread > CreateThread

AfxBeginThread:这是MFC中的Afx系列函数,一个在MFC中创建线程的全局函数。

封装了_beginthreadex,因此,MFC程序,尽量用该函数。

CWinThread:UI线程,能接收消息,需要调用AfxBeginThread创建线程。

AfxBeginThread(RUNTIME_CLASS(MyThread))

二、部分参数介绍

dwStackSize:线程堆栈大小,使用0采用默认设置,默认为1024K,所以默认只能创建不到2048个线程(2G内存).windows会根据需要动态增加堆栈大小。

lpThreadAttributes:线程属性。

lpStartAddress:指向线程函数的指针。

lpParameter:向线程函数传递的参数。

dwCreationFlags:线程标志,CREATE_SUSPENDED表示创建一个挂起的线程,0表示创建后立即激活线程。

lpThreadId,先线程的ID(输出参数)

转载于:https://my.oschina.net/shanlilaideyu/blog/481536

Win32 多线程的创建方法,区别和联系相关推荐

  1. xdl Java_Java多线程的创建方法

    Java 线程类也是一个 object 类,它的实例都继承自java.lang.Thread 或其子类. 可以用如下方式用 java 中创建一个线程,执行该线程可以调用该线程的 start()方法: ...

  2. start()方法和run()方法区别与多线程抢占式运行原理

    目录 start()与run()方法区别 多线程抢占式运行原理 start()与run()方法区别 我们通过一个例子来进行总结,我们写一个利用Thread创建的简单的多线程例子,然后分别执行start ...

  3. Python 技术篇-多线程的2种创建方法,多线程的简单用法,快速上手

    方法一:直接创建 hello 是调用的方法名,hello 如果要传参的话要放到后面的()里,并且后面要有个逗号,没有参数也要加个空的 (). 缺点:不能自由操作线程,不好控制,不会返回对象. impo ...

  4. QT 多线程创建方法及应用实例

    QT 多线程创建方法及应用实例 方法一: (1)创建一个QT应用 (2)创建线程类,继承QThread simplethreadone.h #ifndef SIMPLETHREADONE_H #def ...

  5. 深入浅出Win32多线程程序设计

    引言 从单进程单线程到多进程多线程是操作系统发展的一种必然趋势,当年的DOS系统属于单任务操作系统,最优秀的成员员也只能通过驻留内存的方式实现所谓的"多任务",而如今的Win32操 ...

  6. Win32 多线程学习总结

    Win32多线程编程学习心得 http://blog.csdn.net/jonathan321/article/details/50782832 博客原文地址:http://jerkwisdom.gi ...

  7. Win32多线程编程(3) — 线程同步与通信

    一.线程间数据通信 系统从进程的地址空间中分配内存给线程栈使用.新线程与创建它的线程在相同的进程上下文中运行.因此,新线程可以访问进程内核对象的所有句柄.进程中的所有内存以及同一个进程中其他所有线程的 ...

  8. Win32多线程编程(1) — 基础概念篇

    内核对象的基本概念 Windows系统是非开源的,它提供给我们的接口是用户模式的,即User-Mode API.当我们调用某个API时,需要从用户模式切换到内核模式的I/O System Servic ...

  9. 深入浅出Win32多线程程序设计之线程通信

    简介 线程之间通信的两个基本问题是互斥和同步. 线程同步是指线程之间所具有的一种制约关系,一个线程的执行依赖另一个线程的消息,当它没有得到另一个线程的消息时应等待,直到消息到达时才被唤醒. 线程互斥是 ...

最新文章

  1. 活动直播丨“2021智源论坛”邀您见证智源三年成绩与展望
  2. canvas学习笔记(下篇) -- canvas入门教程--保存状态/变形/旋转/缩放/矩阵变换/综合案例(星空/时钟/小球)...
  3. Freemaker FTL指令常用标签及语法
  4. Java学习笔记8-1——汇编语言入门
  5. 绿盟科技鸿蒙系统,华为 X 绿盟科技,打造“云原生安全新生态”
  6. python 图书馆管理程序下载_图书管理系统 免费开源代码 开发,分享 - CodeForge.cn...
  7. Servlet 快速开始 表单中文字段
  8. yii2 错误处理
  9. 大数据应用及其解决方案(完整版)
  10. 【腾讯云】企业认证题库200题
  11. 综述|线结构光中心提取算法研究发展
  12. php物联网智能家居系统源代码,基于物联网技术的智能家居控制系统设计方案
  13. nodejs 写入html,html2markdownnodejs也能写爬虫?记一次blog迁移至Ghost
  14. 吃白菜一样用micropython玩esp32(三)—— 触摸按键、ADC
  15. carbon安装win7 thinkpad x1_ThinkPad X1 carbon笔记本Win7重装系统步骤详细教程。 重装系统...
  16. 微信小程序设置页面全局背景色
  17. Python爬虫实战—vmgrils图片网站
  18. 富士通南大实习五月记
  19. SNIPER: Efficient Multi-Scale Training
  20. 官方解释:Windows Vista和OpenGL

热门文章

  1. centos7 python3.6升级到3.7_Centos7下把python 2.7升级到python 3.6(升级过程遇到的一些相关问题)...
  2. response.setcharacterencoding 报错是缺哪个包_出街,你缺的是这一款包包...
  3. linux ssh服务的优化,SSH服务端配置、优化加速、安全防护
  4. mysql的联合索引_mysql联合索引
  5. python实现滑块验证功能_python3.8.1+selenium实现登录滑块验证功能
  6. Zookeeper分布式一致性原理(十):Zookeeper在大型分布式系统的应用
  7. socket Php 粘包,python3 tcp的粘包现象和解决办法解析
  8. $.post把表单对象传递过去_第二章 第三节 Request请求对象详解
  9. 接口超时后程序还会继续执行嘛_答网友问:分析一段STL程序,并就如何读懂一段程序谈几点感想...
  10. Redis集群架构搭建详解