一、函数解释

(参见MSDN)

The CreateThread function creates a thread to

execute within the virtual address space of the calling

process.

To create a thread that runs in the virtual address space of

another process, use the CreateRemoteThread function.

HANDLE CreateThread(

LPSECURITY_ATTRIBUTES , SIZE_T , LPTHREAD_START_ROUTINE , LPVOID , DWORD , LPDWORD );

Parameters

lpThreadAttributes

[in] Pointer to a SECURITY_ATTRIBUTES structure that

determines whether the returned handle can be inherited by child

processes. If lpThreadAttributes is NULL, the handle cannot

be inherited.

The lpSecurityDescriptor member of the structure

specifies a security descriptor for the new thread. If

lpThreadAttributes is NULL, the thread gets a default

security descriptor. The ACLs in the default security descriptor

for a thread come from the primary token of the creator.

Windows

XP/2000/NT:The ACLs in the

default security descriptor for a thread come from the primary or

impersonation token of the creator. This behavior changed with

Windows XP SP2 and Windows

Server 2003.

dwStackSize

[in] Initial size of the stack, in bytes. The system rounds

this value to the nearest page. If this parameter is zero, the new

thread uses the default size for the executable. For more

information, see Thread Stack Size.

lpStartAddress

[in] Pointer to the application-defined function to be executed

by the thread and represents the starting address of the thread.

For more information on the thread function, see ThreadProc.

lpParameter

[in] Pointer to a variable to be passed to the thread.

dwCreationFlags

[in] Flags that control the creation of the thread. If the

CREATE_SUSPENDED flag is specified, the thread is created in a

suspended state, and will not run until the ResumeThread function is called. If this value is zero,

the thread runs immediately after creation.

If the STACK_SIZE_PARAM_IS_A_RESERVATION flag is specified, the

dwStackSize parameter specifies the initial reserve size of

the stack. Otherwise, dwStackSize specifies the commit

size.

Windows 2000/NT and Windows

Me/98/95:The

STACK_SIZE_PARAM_IS_A_RESERVATION flag is not

supported.

lpThreadId

[out] Pointer to a variable that receives the thread

identifier. If this parameter is NULL, the thread identifier is not

returned.

Windows

Me/98/95:This parameter may

not be NULL.

Return Values

If the function succeeds, the return value is a handle to the

new thread.

If the function fails, the return value is NULL. To get extended

error information, call GetLastError.

Note that CreateThread may succeed even if

lpStartAddress points to data, code, or is not accessible.

If the start address is invalid when the thread runs, an exception

occurs, and the thread terminates. Thread termination due to a

invalid start address is handled as an error exit for the thread's

process. This behavior is similar to the asynchronous nature of

CreateProcess, where the process is created even if it

refers to invalid or missing dynamic-link libraries (DLLs).

Windows

Me/98/95:CreateThread

succeeds only when it is called in the context of a 32-bit program.

A 32-bit DLL cannot create an additional thread when that DLL is

being called by a 16-bit program.

二、使用实例

1.定义的全局变量

DWORD WINAPI ClientThread(LPVOID

lpParam);

struct ClientInfo

{

SOCKET sock;

SOCKADDR_IN clientAddr;定义地址族

};

2.使用方法

HANDLE

hThread;

DWORD dwThread;

struct ClientInfo

*pClientInfo=NULL;

pClientInfo=(struct ClientInfo

*)malloc(sizeof(struct ClientInfo));

hThread =

CreateThread(NULL,0,ClientThread,(LPVOID)pClientInfo,0,&dwThread);

//free(pClientInfo);

if(hThread==NULL)

{

AfxMessageBox("Thread

Creat Failed!\n");

return;

}

CloseHandle(hThread);

3.线程函数的实现

DWORD WINAPI ClientThread(LPVOID

lpParam)

{

struct ClientInfo *pClinetInfo=(struct ClientInfo

*)lpParam;

SOCKET sock

= pClinetInfo->sock;

SOCKADDR_IN

addrClient=pClinetInfo->clientAddr;

free(lpParam);

CTCPServerDlg

*dlg=(CTCPServerDlg*)AfxGetApp()->GetMainWnd();

while(1)

{

.....

Sleep(200);

}

return 0;

C语言createthread函数详解,CreateThread()使用实例相关推荐

  1. c语言memset对应java,C语言memset函数详解

    C语言memset函数详解 memset() 的作用:在一段内存块中填充某个给定的值,通常用于数组初始化与数组清零. 它是直接操作内存空间,mem即"内存"(memory)的意思. ...

  2. R语言which函数详解以及Rcpp改写

    R语言which函数详解以及Rcpp的改写 引言 which 函数的介绍 which函数的一些小例子 1 2 which函数的改进以及时间对比 引言 首先来介绍一下R语言which函数的作用:whic ...

  3. 【C语言】函数详解(入门到进阶)

    目录 前言 一.什么是函数 二.函数的构成 三.函数的调用和声明 四.函数的参数 五.函数的递归 总结 写在后面 前言 最近帮家里的小朋友整理一些学习C语言的知识点 有整体入门基础文章--[C语言]拯 ...

  4. [C语言] scanf 函数详解多组输入法和gitee 自荐

    自荐 gitee>>> C语言学习练习: C语言学习练习 上面是我的gitee C语言仓库链接,欢迎大家来看看我写的代码.!(◦˘ ³(♡ŐωŐ♡)(给大家推荐gitee(码云)这个 ...

  5. C语言strlen函数详解

    strlen函数详解 一.strlen函数简介 1.函数原型 2.注意事项 二.strlen函数模拟实现 1.计数器方式 2.递归实现 3.指针-指针实现 一.strlen函数简介 1.函数原型 si ...

  6. c语言 指针函数 详解,[NOTE-C]C语言指针详解(一)

    C语言指针让一切想法变成可能,强转和指针可以看做一项呼风唤雨的利器,但是C语言中指针应用又需要格外的小心,其更灵活的利用内存,因为不当的应用可能引起各种异常,这篇文章就是让我们一起来认识C指针,更好的 ...

  7. C语言scanf函数详解

    函数名: scanf  功 能: 运行格式化输入  用 法: int scanf(char *format[,argument,...]); scanf()函数是通用终端格式化输入函数,它从标准输入设 ...

  8. C语言main()函数详解

    C的设计原则是把函数作为程序的构成模块.main()函数称之为主函数,一个C程序总是从main()函数开始执行的. 一.main()函数的形式 在最新的 C99 标准中,只有以下两种定义方式是正确的: ...

  9. fgets函数及其用法,C语言fgets函数详解

    虽然用 gets() 时有空格也可以直接输入,但是 gets() 有一个非常大的缺陷,即它不检查预留存储区是否能够容纳实际输入的数据,换句话说,如果输入的字符数目大于数组的长度,gets 无法检测到这 ...

最新文章

  1. 习惯几乎可以绑住一切,只是不能绑住偶然。比如那只偶然尝了鲜血的老虎。...
  2. Android开发框架afinal实践
  3. 面试 -- 多线程( 一) -- 基础
  4. C语言字符串左右排序交换
  5. Java基础---数组练习(最大值、最小值的索引)
  6. jvm常用监控命令总结
  7. 通过分析nginx的日志来过滤出访问过于频繁的IP地址,然后添加到nginx的blockip.conf,并重启nginx...
  8. Flutter实战之Flutter应用生命周期 AppLifecycleState浅析
  9. Gambit建模中split的用法
  10. Unity 使用Shader实现序列帧动画
  11. 码农小汪之理解Java注解。
  12. 计算机参数配置解读,教你看懂电脑配置参数,了解组装电脑基本知识
  13. raspberrypi
  14. 彻底理解Linux的各种终端类型以及概念
  15. 使用c语言编程首先要新建,【C语言编程入门系列】—— 第三章,编写第一个C语言程序!...
  16. linux主分区扩容
  17. python找出列表中最长/短的字符串及他们的长度、下标
  18. python爬取12306列车信息自动抢票并自动识别验证码(一)列车数据获取篇
  19. c++框架crow搭建web服务入门
  20. 京东商城暂停所有地铁自提点:或因租金成本高

热门文章

  1. SLAM 整体性总结
  2. 修改Redis端口号以及解决修改完端口号但是没有生效的问题
  3. AutoCAD2019+vs2019+C# 二次开发学习笔记day01(持续更新)
  4. xp系统打开计算机硬盘分区,xp系统如何使用自带磁盘分区安装方法
  5. 伦敦春日观光好去处:奇斯威克庄园和厨房花园
  6. php 采集qq头像,自带采集QQ头像表白墙源码
  7. ssh 常用远程命令
  8. Oracle 排序问题
  9. oracle 排序:order by
  10. UbuntuKylin常用快捷键