zoukankan      html  css  js  c++  java
  • CreateThread简单那多线程编程

    CreateThread简单那多线程编程

    作者:vpoet
    mail:vpoet_sir@163.com



    在进行多任务处理的时候我们往往会用到多线程技术,多线程理论上是多个线程同事处理不同的工作,但是这只针对多核的CPU而言
    但是对于单核CPU多线程往往实现的方式是:CPU为各个线程分配时间片,让各个线程循环的执行,但是这个时间片又很短,所以给我
    们只管的映像就好像是多个线程在同时工作。

    本文主要使用windows API CreateThread函数进行多线程演示。


    首先看看MSDN上关于CreateThread函数的说明:

    参数说明:
    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. 
    该参数决定子线程是否继承父进程的安全属性,如果为NULL,表示不继承。关于SECURITY_ATTRIBUTES结构体请查询MSDN

    dwStackSize [in] Specifies the initial commit size of the stack, in bytes. The system rounds this value to the nearest page. If this value is zero, or is smaller than the default commit size, the default is to use the same size as the calling thread. For more information, see Thread Stack Size
    该参数指定线程堆栈大小,如果该值为零则使用默认的线程堆栈大小
    lpStartAddress [in] Pointer to the application-defined function of type LPTHREAD_START_ROUTINE 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] Specifies a single parameter value passed to the thread. 
    该参数为传递给线程函数的参数
    dwCreationFlags [in] Specifies additional 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. At this time, no other values are supported. 
    该参数为创建标识,如果为零,那么线程创建后会立即执行,若为CREATE_SUSPENDED则线程创建后则会停止
    lpThreadId [out] Pointer to a variable that receives the thread identifier. 
    该参数为创建线程的线程ID,线程ID就是唯一标识该线程的一个数字





    那么现在我们来写一个最简单的多线程例子:
    #include <windows.h>
    #include "stdio.h"
    
    DWORD WINAPI ThreadOne(LPVOID lpParameter)
    {
    	printf("ThreadOne is Runing
    ");
    	Sleep(100);
    	return 0;
    }
    
    
    int main()
    {
    	HANDLE HOne;
    	printf("***********************vpoet******************
    ");
    	HOne=CreateThread(NULL,0,ThreadOne,NULL,0,NULL);
    	printf("ThreadOne Begin!
    ");
    	CloseHandle(HOne);
    	printf("MainThread Over!
    ");
    	return 0;
    }
    运行结果:
    咦!是不是看出来什么问题了,子线程貌似没有运行主线程就退出了,这是怎么回事呢,这说明当开启子线程后还未来得及运行主线程就已经执行完毕,并且return了,那么此时整个程序也就退出,当然也就不会再运行子线程了。那么我们应该怎么控制了,既然子线程来不及执行,那么我就让主线程多运行一会儿等到子线程执行完了之后主线程再退出。是不是很有道理,恩,我也觉得。
    那么我们在
    printf("MainThread Over!
    ");

    之前再加入一句代码
    Sleep(1000)

    让主线程延时1s等待子线程的执行。因为1s足够让子线程执行完毕了所以这次程序就会在子线程运行结束后正常退出了。运行结果如下:
    看吧,这下是正常退出了吧。

    当然还有其他方法来等待子线程正常执行并返回,对了还有个HOne这玩意儿还没有介绍,
    Handle其实就是一个句柄对象,就像是一个结构体或者类一样,只不过它是内核对象,由内核
    自己维护。为啥需要CloseHand()呢 百度百科是这样说的:
    关闭一个内核对象。其中包括文件、文件映射、进程、线程、安全和同步对象等。在CreateThread成功之后会返回一个hThread的handle,且内核对象的计数加1,CloseHandle之后,引用计数减1,当变为0时,系统删除内核对象。
    若在线程执行完之后,没有调用CloseHandle,在进程执行期间,将会造成内核对象的泄露,相当于句柄泄露,但不同于内存泄露,这势必会对系统的效率带来一定程度上的负面影响。但当进程结束退出后,系统会自动清理这些资源。


    那么我们就需要知道线程到底是什么时候退出的呢,然后在其退出的时候把该线程句柄Close掉。
    这里要介绍一个API叫WaitForSingleObject;
    我们MSDN一下:

    MSDN这样说该函数只有在以下两种情况下才返回:
    1.指定的对象有信号状态时
    2.等待超时

    那到底是什么意思呢,别着急,我们再看看参数:
    hHandle [in] Handle to the object. For a list of the object types whose handles can be specified, see the following Remarks section.

    If this handle is closed while the wait is still pending, the function's behavior is undefined. 


    该参数是一个句柄对象,如果句柄已经Close掉后还在WaitForSingleObject那么这种情况是未定义的,未定义是啥意思了就是会有意想不到的效果,
    说白了就是重大的隐藏的BUG

    dwMilliseconds [in] Specifies the time-out interval, in milliseconds. The function returns if the interval elapses, even if the object's state is nonsignaled. If dwMilliseconds is zero, the function tests the object's state and returns immediately. If dwMilliseconds is INFINITE, the function's time-out interval never elapses. 
    该参数是指定超时间隔,如果参数为零则函数马上返回,如果参数为INFINITE,函数会一直等待。


    另外该函数的返回值有主要有两种状态:
    WAIT_OBJECT_0 The state of the specified object is signaled. 表示句柄为有信号状态时候返回
    WAIT_TIMEOUT The time-out interval elapsed, and the object's state is nonsignaled. 表示等待超时返回

    现在我们去掉主函数的Sleep延时1s的代码,在CreateThread()后面加上一句:
    WaitForSingleObject(HOne,INFINITE);

    现在我们再看看运行结果:


    果然正确,现在明白WaitForSingleObject(HOne,INFINITE);的意思了吧

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    Linux操作系统基础(四)保护模式内存管理(2)【转】
    Linux-0.00运行环境搭建【转】
    一个简单多任务内核实例的分析【转】
    makefile中的自动化变量 【转】
    ARM处理器的寄存器,ARM与Thumb状态,7中运行模式 【转】
    Linux中__init、__devinit等内核优化宏【转】
    中断的理解
    linux rtc 接口【转】
    HDU1506 ( Largest Rectangle in a Histogram ) [dp]
    angularJS使用$watch监控数据模型的变化
  • 原文地址:https://www.cnblogs.com/vpoet/p/4659576.html
Copyright © 2011-2022 走看看