这三个函数都可以创建新的线程,但都是如何创建的呢?当然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语言中重要函数

    gets函数,从标准输入读取一行文本,一行输入由一串字符组成,以一个换行符结尾: gets函数丢弃换行符,并在该行的末尾存储一个NUL字符(类似‘\0’), 然后返回一个非NULL值. 当gets函数 ...

  2. JavaScript之将JS代码放在什么位置最合适

    1.放到<head></head>标签里面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional/ ...

  3. iOS实践02

    第二天了,上了一天课,软件测试.数据挖掘.概率论,晚上了才有时间捣鼓捣鼓程序. 今天只是简单的做了一点.觉得自己思考的写不出来,只能简单的写一个过程,不像第一次写这个,少了很多思考的. 1.完善tab ...

  4. BZOJ 2208: [Jsoi2010]连通数( DFS )

    n只有2000,直接DFS就可以过了... -------------------------------------------------------------------------- #in ...

  5. 简单十步让你全面理解SQL

    很多程序员认为SQL是一头难以驯服的野兽.它是为数不多的声明性语言之一,也因为这样,其展示了完全不同于其他的表现形式.命令式语言. 面向对象语言甚至函数式编程语言(虽然有些人觉得SQL 还是有些类似功 ...

  6. Linux 中 java 访问 windows共享目录

    有两个方案: 1.将windows共享目录,挂载到linux系统下,通过使用本地目录访问windows共享目录 2.通过samba的java实现包,不过需要开个windows共享目录的账户  http ...

  7. codeforces 628F. Bear and Fair Set 网络流

    题目链接 F. Bear and Fair Set time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  8. OutputDebugString 输出信息到调试器

    #include <Windows.h>#include <stdio.h>#include <stdarg.h> void __cdecl odprintf(co ...

  9. fork出的子进程和父进程的继承关系【转载】

    [原文地址]http://blog.163.com/dengjingniurou@126/blog/static/53989196200962924412524/ fork出的子进程和父进程的继承关系 ...

  10. UIWebView的三种加载方式

    一.使用UIWebView 将web content 嵌入到应用上. API提供了三种方法: - (void)loadRequest:(NSURLRequest *)request; - (void) ...