加文

希望是美好的……
随笔 - 0, 文章 - 209, 评论 - 0, 引用 - 0
数据加载中……

多线程测试

// one.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
using namespace std;

BOOL repeat = true;

DWORD WINAPI MyThread1(LPVOID lpParameter)
{
    while(repeat)
    {
        cout<<"how are you ?"<<endl;
        Sleep(1);
    }
    DWORD exitCode;
    ExitThread(exitCode);
    return 0;
}
DWORD WINAPI MyThread2(LPVOID lpParameter)
{
    while(repeat)
    {
        cout<<"very well !"<<endl;
        Sleep(1);
    }
    DWORD exitCode;
    ExitThread(exitCode);
    return 0;
}
DWORD WINAPI MyThread3(LPVOID lpParameter)
{
    while(repeat)
    {
        cout<<"very well !"<<endl;
        Sleep(1);
    }
    DWORD exitCode;
    ExitThread(exitCode);
    return 0;
}
DWORD WINAPI KillThread(LPVOID lpParameter)
{
    repeat = false;
    return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
    HANDLE handle1,handle2,handle3;
    DWORD dw1,dw2,dw3;
    handle1 = CreateThread(NULL,0,MyThread1,NULL,0,&dw1);


    if(handle1== NULL)
    {
        cout<<"线程1创建失败!!"<<endl;
        return -1;
    }


    handle2 = CreateThread(NULL,0,MyThread2,NULL,0,&dw2);
    if(handle2== NULL)
    {
        cout<<"线程2创建失败!!"<<endl;
        return -1;
    }
    handle3 = CreateThread(NULL,0,MyThread3,NULL,0,&dw3);
    if(handle3== NULL)
    {
        cout<<"线程1创建失败!!"<<endl;
        return -1;
    }
    CloseHandle(handle1);
    CloseHandle(handle2);
    CloseHandle(handle3);
    getchar();
    return 0;
}

posted on 2012-04-23 23:49 加文 阅读(330) 评论(0)  编辑 收藏 引用 所属分类: C++


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理