这三个函数都可以创建新的线程,但都是如何创建的呢?当然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. [C# 基础知识系列]专题六:泛型基础篇——为什么引入泛型

    引言: 前面专题主要介绍了C#1中的2个核心特性——委托和事件,然而在C# 2.0中又引入一个很重要的特性,它就是泛型,大家在平常的操作中肯定会经常碰到并使用它,如果你对于它的一些相关特性还不是很了解 ...

  2. Head First设计模式学习笔记

    最近在学C++,直接语法之后觉得不太有意思,直接做项目又觉得太肤浅.正好之前一直想学设计模式来着,可惜之前一直在玩C,所以没有机会深入学习,于是决定用C++把设计写一遍.看了点GOF的<设计模式 ...

  3. 编译ycm库

    在安装完YCM之后,重新打开vim还会出现如下的报错信息:ycm_client_support.[so|pyd|dll] and ycm_core.[so|pyd|dll] not detected; ...

  4. BZOJ 1927: [Sdoi2010]星际竞速(最小费用最大流)

    拆点,费用流... ----------------------------------------------------------------------------- #include< ...

  5. Volley 设置 RetryPolicy 不起作用, 重复提交

    RT, Google后有的说是 将超时时间设置为0, 但是还是会重试提交, 解决方案如下: static HurlStack stack = new HurlStack(){ @Override pr ...

  6. Python之路Day2

    -->the start 养成好习惯,每次上课的内容都要写好笔记. 第二天内容主要是熟悉int.long.float.str.list.dict.tuple这几个类的内建方法. 对于Python ...

  7. [LeetCode]题解(python):007-Reverse Integer

    题目来源: https://leetcode.com/problems/reverse-integer/ 题意分析: 这道题目很简单,就是将一个数反转,123变321,-123变321. 题目思路: ...

  8. 将默认首页设置成index.do的方法

    变态欺骗法,今天csdn一个前辈的,学习了,公司服务器是weblogic的,也可以欺骗. 但是我又非常迫切.非常盼望.非常渴望使用index.do做首页,怎么办? Tomcat中用一段注释: When ...

  9. (C#)Windows Shell 编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令

    原文(C#)Windows Shell 编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令 (本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) 接上一节:( ...

  10. 2014第35周三jquery最近用到的内容总结

    1.文档加载后执行: $(document).ready(function(){//onload();}); 或$(function(){//onload();}) 2. 选择器使用: $(" ...