这三个函数都可以创建新的线程,但都是如何创建的呢?当然MSDN文档最权威:

Creates a thread to execute within the virtual address space of the calling process.

在调用进程的虚拟地址空间里创建一个线程用CreateThread;

To create a thread that runs in the virtual address space of another process, use the CreateRemoteThread function.

如果在另一进程的虚拟地址空间创建线程用CreateRemoteThread

具体参数:

HANDLE WINAPI CreateThread(
_In_opt_   LPSECURITY_ATTRIBUTES lpThreadAttributes,
_In_       SIZE_T dwStackSize,
_In_       LPTHREAD_START_ROUTINE lpStartAddress,
_In_opt_   LPVOID lpParameter,
_In_       DWORD dwCreationFlags,
_Out_opt_  LPDWORD lpThreadId
);

参数:

lpThreadAttributes [in, optional]//可选,一般为NULL

一个指向SECURITY_ATTRIBUTES的指针,如果设为NULL,表示handle不能被子进程继承。

dwStackSize [in]

栈的初始值,以字节为单位。系统把这个值分到最近的页。如果设为0,新线程会用默认值来执行。

lpStartAddress [in]

指向线程执行函数的指针。这个指针代表线程开始的地址。

lpParameter [in, optional]

传给线程的变量。(注意不能是局部变量)

dwCreationFlags [in]

一些控制标志。

Value Meaning
0

The thread runs immediately after creation.线程立即被创建

CREATE_SUSPENDED
0x00000004

The thread is created in a suspended state, and does not run until the ResumeThread function is called.

STACK_SIZE_PARAM_IS_A_RESERVATION
0x00010000

The dwStackSize parameter specifies the initial reserve size of the stack. If this flag is not specified, dwStackSize specifies the commit size.

lpThreadId [out, optional]

传回线程标志,如果设为NULL,将不被传回。

返回值

如果函数成功,将返回新线程的handle,失败了返回NULL,并且可以用GetLastError来获取错误信息。

注意

The number of threads a process can create is limited by the available virtual memory. By default, every thread has one megabyte of stack space. Therefore, you can create at most 2,048 threads. If you reduce the default stack size, you can create more threads. However, your application will have better performance if you create one thread per processor and build queues of requests for which the application maintains the context information. A thread would process all requests in a queue before processing requests in the next queue.

进程创建线程数目是由可用虚拟地址限制的。默认的,一个线程有1M的栈空间。如果是32位的机子,一个进程有2G内存空间【关于这点估计得去看CreateProcess了】,那也就是可以创建2×1024个线程。如果减少默认栈空间,可以创建更多的线程。然而,如果一个处理器创建一个线程,并创建请求队列以维护上下文信息,软件将会有更好的性能【看来专家也不推荐创建很多线程啊,上下文切换开销大】。一个线程将会在处理下一队列请求前处理在队列的所有请求【这句不是很理解】。

可以用OpenThread来得到线程access。

如果一个线程创建时没有用CREATE_SUSPENDED参数,那么就创建在运行态,线程会在CreateThread函数返回前就开始运行,特别地,在调用者得到handle和线程标识前就开始运行。

线程开始于lpStartAddress所指的地址。如果这个函数返回,返回值被用来默认地调用ExitThread函数。可以用theGetExitCodeThread来得到线程的返回值。

还可以用THREAD_PRIORITY_NORMAL来表示线程优先级。

当一个线程终止了,线程对象达到激发态,任何等待线程的对象都会被激发。

线程对象保持在系统中,知道线程结束或者所有的handle都关闭了。【因为handle是引用计数】

有一些限制:

  • 在进程启动和DLL初始化时,线程可以被创建,但是线程会在DLL初始化之后被执行。
  • 在DLL初始化时进程中只能有一个线程。
  • ExitProcess不能完成,知道在他们的DLL没有线程来。

在C运行时库时用_beginthreadex_endthreadex代替CreateThread 和 ExitThread进行线程管理;这要求多线程版本的运行时库。如果一个线程用CreateThread调用CRT,CRT了能在低内存情况下终止进程。

好了,把CreateThread的介绍翻译完了,也该下班了,哈哈。翻译不怎么样,有的地方还不怎么理解。

CreateThread与_beginthread, _beginthreadex创建线程的基本概念和区别的更多相关文章

  1. CreateThread与_beginthread, _beginthreadex创建线程的基本概念和区别(1)

    这三个函数都可以创建新的线程,但都是如何创建的呢?当然MSDN文档最权威: Creates a thread to execute within the virtual address space o ...

  2. _beginthreadex创建线程,立即执行?

    一个线程创建后,并不是立马就执行,而是等时间片到来后才执行...  C++ Code  12345678910111213141516171819202122232425262728293031323 ...

  3. VC.【转】采用_beginthread/_beginthreadex函数创建多线程

    https://blog.csdn.net/cbnotes/article/details/8331632 还可以看这个网址的内容:[多线程]VC6使用_beginthread开启多线程的方法-技术宅 ...

  4. WINDOWS-API:关于线程CreateThread,_beginthead(_beginthreadex),AfxBeginThread

    [转]windows多线程编程CreateThread,_beginthead(_beginthreadex)和AfxBeginThread的区别 在Windows的多线程编程中,创建线程的函数主要有 ...

  5. (转)CreateThread与_beginthread,内存泄漏为何因(原帖排版有些不好 ,所以我稍微整理下)

            在写c++代码时,一直牢记着一句话:决不应该调用CreateThread. 应该使用Visual   C++运行时库函数_beginthreadex.好像CreateThread函数就 ...

  6. Java线程—-Runnable和Callable的区别和联系

    Java 提供了三种创建线程的方法 1.继承Thread接口 public class Thread2Thread { public static void main(String[] args) { ...

  7. 01创建线程CreateThread和_beginthreadex

    Windows多线程之线程创建 一. 线程创建函数 CreateThread 1. 函数原型 HANDLE WINAPI CreateThread( _In_opt_ LPSECURITY_ATTRI ...

  8. windows多线程(十一) 更安全的创建线程方式_beginthreadex()

    一.原因分析 CreateThread()函数是Windows提供的API接口,在C/C++语言另有一个创建线程的函数_beginthreadex(),我们应该尽量使用_beginthreadex() ...

  9. windows多线程(一) 创建线程 CreateThread

    一 线程创建函数 CreateThread 修改说明: 这里 说了另一种创建线程方法,使用_beginthreadex()更安全的创建线程,在实际使用中尽量使用_beginthreadex()来创建线 ...

随机推荐

  1. Android -- DiskLruCache

    DiskLruCache 创建一个磁盘缓存对象: public static DiskLruCache open(File directory, int appVersion, int valueCo ...

  2. Jquery 让contains不区分大小写

    默认的Jquery contains方法是区分大小写的,以下方法可以使contains不区分大小写(在匹配时将要匹配的元素内容全部转换成小写再进行匹配) // NEW selector jQuery. ...

  3. HTTP协议中源端口和目标端口的问题

    [提问] How is source port for HTTP determined? Is there ever collision in NAT?   I know that when a HT ...

  4. pip通过requirements.txt安装依赖

    pip install -t . -r requirements.txt requirements.txt Keras==2.0.8 gensim torchvision opencv-python ...

  5. ASP入门(十一)-Session小案例

    一般来说,在实际开发中,对于 Session 对象使用最多的就是用户登录部分了,这个案例将简单模拟一个用户登录表单.用户是否登录的判断以及用户退出的一系列功能,它一共分了以下几个页面. Login.a ...

  6. Android Studio 上传aar(Library)到JCenter

    目的 这篇文章介绍通过Gradle把开源项目发布到公共仓库JCenter中,发布自己的android library(也就是aar)到公共的jcenter仓库. 为什么选择JCenter,因为JCen ...

  7. tail 命令

    转自:http://www.cnblogs.com/peida/archive/2012/11/07/2758084.html tail 命令从指定点开始将文件写到标准输出.使用tail命令的-f选项 ...

  8. Ado.Net,关于DataSet和DataTable

    DataSet和DataTable的 区别与联系 1.简要说明二者关系 在我们编写代码的时候从数据库里取出数据,填充到dataset里,再根据表的名字,实例化到 DataTable 中. ●注意如下  ...

  9. 微软BI 之SSRS 系列 - 如何实现报表标签的本地化 - 中文和英文的互换

    SSRS 中并没有直接提供本地化的配置方式,因此在 SSRS 中实现本地化,比如有英文标题还有可选的中文标题,就需要通过其它的方式来解决. 比如默认是这样的英文标题 - 但是本地中方用户可能比较喜欢看 ...

  10. js setTimeout setInterval 第三个参数说明

    1.api setTimeout: https://developer.mozilla.org/zh-CN/docs/Web/API/Window/setTimeout var timeoutID = ...