Chinaunix首页 | 论坛 | 博客
  • 博客访问: 40343
  • 博文数量: 32
  • 博客积分: 1326
  • 博客等级: 中尉
  • 技术积分: 330
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-25 18:13
文章分类

全部博文(32)

文章存档

2010年(32)

我的朋友
最近访客

分类: WINDOWS

2010-06-24 20:53:44

#include "stdafx.h"
#include <afxwin.h>
#include <windows.h>

HANDLE hEvent = NULL;
char g_cArray[10];

UINT ThreadProcFirst(LPVOID pParam);
UINT ThreadProcSecond(LPVOID pParam);

int main(int argc, char* argv[])
{
    /*创建一个可以自动复位的事件内核对象*/
    hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    
    /*置位*/
    SetEvent(hEvent);
    
    AfxBeginThread(ThreadProcFirst, NULL);
    AfxBeginThread(ThreadProcSecond, NULL);
    
    Sleep(500);
    
    CString strResult = CString(g_cArray);
    AfxMessageBox(strResult);
    return 0;
}

UINT ThreadProcFirst(LPVOID pParam)
{
    /*无限等待hEvent的复位*/
    WaitForSingleObject(hEvent, INFINITE);
    printf("First");
    for (int i = 0; i < 10; ++i)
    {
        g_cArray[i] = 'a';
        Sleep(1);
    }
    
    SetEvent(hEvent);
    return 0;
}

UINT ThreadProcSecond(LPVOID pParam)
{
    WaitForSingleObject(hEvent, INFINITE);
    printf("Second");
    for (int i = 0; i < 10; ++i)
    {
        g_cArray[10-i-1] = 'b';
        Sleep(1);
    }
    
    SetEvent(hEvent);
    return 0;
}


阅读(368) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~