这三个函数都可以创建新的线程,但都是如何创建的呢?当然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. Datetimepicker配置参数

    jquery的datetimepicker时间控件除了样式有点不太美观,功能性还是相当强大的. 在正常情况下input的type应该设置为"text",可点击又可输入(mask,e ...

  2. 转:git设置过滤忽略的文件或文件夹

    from: https://www.cnblogs.com/foohack/p/4629255.html git设置过滤忽略的文件或文件夹   我们一般向代码仓库提交项目的时候,一般需要忽略编译生成的 ...

  3. PowerShell获取当前用户的权限

      function Get-CurrentUserRoles {   $SecurityPrinciple = New-Object -TypeName System.Security.Princi ...

  4. [Canvas]首个小游戏告成

    英雄在地图上射箭杀怪兽,杀完了就胜利了. 点此下载程序试玩. 图例: 代码: <!DOCTYPE html> <html lang="utf-8"> < ...

  5. java interface 默认值

    /* * Hibernate, Relational Persistence for Idiomatic Java * * Copyright (c) 2010, Red Hat Inc. or th ...

  6. PHP的异常以及异常存在的意义

    php的try catch与其它语言的try catch相比有许多不同,而且用起来相对比较不爽. php中,如果你制作的站点相对较大,同时模块化,并且在错误处理机制上有一套自己的处理机制,可以尝试使用 ...

  7. SDE注册版本失败,仅支持一个空间列

    如果直接编辑SDE要素类与要素可以不需要版本,使用默认版本,如果要让用户通过界面编辑,即使用开启编辑.保存编辑和停止编辑,就需要注册为版本,而在注册版本弹出如下错误: 正如错误所说,一个要素类或shp ...

  8. Linux中awk命令的简单用法

    一.用例1: cat /proc/meminfo|grep "MemTotal"|awk '{print $2}' 说明,$2表示第2位,$0表示全部,如需表示$,可用$$转义.

  9. IntelliJ IDEA安装主题详细步骤

    主题下载网址:http://color-themes.com/?view=index 本文测试的软件版本:IntelliJ IDEA 2016.1.2 一.下载主题包   1 1. 打开上述网址(ht ...

  10. shell 截取变量的字符串(转)

    来自:http://blog.sina.com.cn/s/blog_7c95e5850100zpch.html 假设有变量 var=http://www.linuxidc.com/test.htm 一 ...