多线程练习(2018-09-18)

要求:十个线程打印各自的进程号


#include

#include

#include

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;

}

你可能感兴趣的:(多线程练习(2018-09-18))