Windows系统下等待线程退出的方法

来源:互联网 发布:淘宝市场行情有什么用 编辑:程序博客网 时间:2024/04/28 23:35

Windows系统下等待线程退出的方法

 

示例代码:

#include<process.h>
#include<windows.h>
#include<stdio.h>
#include<conio.h>


DWORD WINAPI ThreadFunc(LPVOID lpParam)
{
    printf("本线程已经结束\n");
    return 0;
}

void main()
{
    int t;    // t 是未知数,是等待用户赋值的
    HANDLE *hThread;
    hThread=NULL;


    for(int i = 0; i < t;  i++)
    {
        DWORD dwThreadId;
        hThread=(HANDLE *)realloc(hThread,sizeof(HANDLE)*i);
        hThread[i]=CreateThread(NULL, 0, ThreadFunc, NULL, NULL, &dwThreadId);
    }


    // WaitXXX  把第三个参数设为 TRUE 就是等所有线程都结束后才返回。
    WaitForMultipleObjects(t, hThread, TRUE, INFINITE);


    getch();
}

------------------------------------

单线程用:
WaitForSingleObject(hThread,  INFINITE);

 

0 0