#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


#include <iostream>
#include <tchar.h>
// TODO: 在此处引用程序要求的附加头文件
#include <windows.h>

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

#include "stdafx.h"
using namespace std;

HANDLE g_hEvent;
unsigned long __stdcall AnotherThread(void*);
HANDLE Create(int&);

int _tmain(int argc, _TCHAR* argv[])
{
	/*
	HANDLE handle=::CreateEvent(NULL,FALSE,TRUE,NULL);
	if(WaitForSingleObject(handle,0)==WAIT_OBJECT_0)
		cout<<"It has--------------"<<endl;
	if(WaitForSingleObject(handle,1000*3)==WAIT_TIMEOUT)
		cout<<"It has**************"<<endl;
	SetEvent(handle);
	if(WaitForSingleObject(handle,INFINITE)==WAIT_OBJECT_0)
		cout<<"It has##############"<<endl;
	CloseHandle(handle);
	*/

	int k=10;
	HANDLE handle=Create(k);
	if(!handle) { cout<<"Create() Error!!"<<endl; return 0;}
	WaitForSingleObject(g_hEvent,INFINITE);
	cout<<"Terminate Thread~~"<<endl;
	CloseHandle(g_hEvent);
	system("pause");
	return 0;
}

unsigned long __stdcall AnotherThread(void* pData)
{
	int k=*(int*)pData;
	for(int i=0;i<5;i++)
	{
		cout<<"i="<<i<<"\tk="<<k<<endl;
		Sleep(1000);
	}
	SetEvent(g_hEvent);
	return 1;
}

HANDLE Create(int& data)
{
	DWORD dwThread;
	HANDLE hThread=::CreateThread(NULL,0,&AnotherThread,&data,0,&dwThread);
	if(!hThread) return NULL;
	g_hEvent=::CreateEvent(NULL,FALSE,FALSE,NULL);
	if(!g_hEvent) return NULL;
	return hThread;
}

/*
i=0     k=10
i=1     k=10
i=2     k=10
i=3     k=10
i=4     k=10
Terminate Thread~~
请按任意键继续. . .
*/