子线程循环 10 次,接着主线程循环 100 次,接着又回到子线程循环 10 次,接着再回到主线程又循环 100 次,如此循环50次,试写出代码。
#include <Windows.h>
#include<stdio.h>
HANDLE g_hThreadEvent;
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
for (int i = 0; i <10; i++)
{
printf("threadid:%d num:%d \r\n", GetCurrentThreadId(), i);
}
SetEvent(g_hThreadEvent);
return 0;
}
int main(int argc, char *argv[])
{
g_hThreadEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
for (int i = 0; i < 50; i++)
{
CreateThread(NULL, 0, ThreadProc, NULL, NULL, nullptr);
WaitForSingleObject(g_hThreadEvent, INFINITE);
for (int j = 0; j < 100; j++)
{
printf_s("threadid: %d xh:%d num:%d \r\n", GetCurrentThreadId(), i, j);
}
ResetEvent(g_hThreadEvent);
}
CloseHandle(g_hThreadEvent);
system("pause");
return 0;
}
posted on 2018-03-15 11:09 fendoudexiaoniao 阅读(190) 评论(0) 编辑 收藏 举报