这三个函数都可以创建新的线程,但都是如何创建的呢?当然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)的更多相关文章

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

    这三个函数都可以创建新的线程,但都是如何创建的呢?当然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. linux核心之进程管理

    进程就是处于执行期的程序(目标码存放在某中介质上).进程并不仅仅局限于一段可执行程序代码,通常还包括其他资源,例如打开的文件,挂起的信号,内核内部数据,处理器状态,一个或多个具有内存映射的内存地址空间 ...

  2. SSIS DB目录设置 (Integration Services Catalogs)

    1.创建SSISDB目录 这里没什么好说的,点击Enable CLR Integration ,然后设置一个加密密码 2. SSIS Catalog设置 Retention Period (days) ...

  3. HDU3863:No Gambling

    Problem Description One day, Flyvan introduced a new game to his two friends, Oregon Maple and Grape ...

  4. 用wfastcgi在IIS下部署Django&Flask

    Django跟Flask在Linux底下都可以很方便地以FastCGI模式部署,貌似IIS下面不很好配置,而且IIS也缺少一个像PHPmanager一样的全自动配置工具,在公司服务器上部署起来颇费周折 ...

  5. IOS 特定于设备的开发:UIDevice

    UIDevice类展示了一些关键的特定于设备的属性,包括使用的iPhone ,Ipad或iPod Touch型号.设备名称.以及OS名称和版本. 他是一种一站式解决方案,用于提取出某些系统详细信息.每 ...

  6. Stack trace对性能的影响

    package ceshi; public class ExceptionTest { public long maxLevel = 20; public static void main(Strin ...

  7. javascript closure 闭包 事件绑定

    先来一个基本的例子 <!-- 实现一段脚本,使得点击对应链接alert出相应的编号 --> <meta http-equiv="Content-Type" con ...

  8. Tomcat+redis+nginx配置

    为客户开发的一个绩效系统,采用了java web的开发方式,使用了一些spring mvc, mybatis之类的框架.相比于oracle ebs的二次开发,这种开发更加灵活,虽然和ebs集成的时候遇 ...

  9. Elasticsearch 单模式下API的增删改查操作

    <pre name="code" class="html">Elasticsearch 单模式下API的增删改查操作 http://192.168. ...

  10. 基于Visual C++2013拆解世界五百强面试题--题5-自己实现strstr

    请用C语言实现字符串的查找函数strstr, 找到则返回子字符串的地址,没有找到返回为空,请用数组操作与指针操作实现 看到题目想到最简单的方法就是母字符串和子字符串比较,如果不同,将指向母字符串的指针 ...