首页 > 其他 > 详细

CreateThread

时间:2020-03-23 10:35:54      阅读:68      评论:0      收藏:0      [点我收藏+]
// 2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "windows.h"
#include "iostream.h"
#include "stdlib.h"
DWORD WINAPI thr1(LPVOID lp)//参数可以给线程传递一些数据,作为内部使用,很重要
{
    while(true)
    {
        cout<<"Hello,thread!____1___"<<endl;
        Sleep(1000);
    }
    return 0;
}

DWORD WINAPI thr2(LPVOID lp)
{
    while(true)
    {
        cout<<"Hello,thread!____2___"<<endl;
        Sleep(1000);
    }
    return 0;
}

VOID threa()
{
    DWORD nid1=0,nid2=0;
    HANDLE hd1=CreateThread(NULL,0,thr1,NULL,0,&nid1);
    WaitForSingleObject(hd1,2000);
    HANDLE hd2=CreateThread(NULL,0,thr2,NULL,0,&nid2);
    CloseHandle(hd1);//关闭句柄,释放资源,并不是结束线程
    CloseHandle(hd2);
}

int main(int argc, char* argv[])
{
    threa();
    getchar();
    return 0;
}





******************************************************************

#include "stdafx.h"
#include "windows.h"
#include "iostream.h"

DWORD WINAPI pro(LPVOID lp)
{
    cout<<"Hello,thread!"<<endl;
    return 0;
}
int main(int argc, char* argv[])
{
    DWORD nid=0;
    HANDLE hd;
    hd=CreateThread(NULL,0,pro,NULL,0,&nid);
    WaitForSingleObject(hd,INFINITE);//如果没有这个函数的话,会出现的情况就是自己创建的线程无法执行
    CloseHandle(hd);
    return 0;
}







//每个进程中都有一个看不见的主线程,在考虑的时候不能遗漏,否则会出现线程调度的问题

 

CreateThread

原文:https://www.cnblogs.com/butchert/p/12550031.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!