要求:十个线程打印各自的进程号
#include <iostream>
#include <process.h>
#include <Windows.h>
using namespace std;
DWORD WINAPI thread_handle(LPVOID lpParameter)
{
cout << "process: " << GetCurrentProcessId() << ' ' << "thread: " << GetCurrentThreadId() << endl;
return 0;
}
int main()
{
int i;
HANDLE buf[10]; //生成句柄
for(i=0;i<10;i++)
{
buf[i] = CreateThread(NULL,0,thread_handle,NULL,0,NULL);
Sleep(10);
}
WaitForMultipleObjects(10, buf, TRUE, INFINITE); //等待十个线程结束
for(i=0;i<10;i++)
{
CloseHandle(buf[i]);
}
system("pause");
return 0;
}