threadsynchr1.cpp
来自「MFC程序开发参考大全 【明日科技】宋坤 刘锐宁 李伟明 【丛 书 名】」· C++ 代码 · 共 58 行
CPP
58 行
// ThreadSynchr1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "windows.h"
int WorkerID = 10;
const int MAXWORKERID = 100;
//Define thread function
DWORD __stdcall ThreadFunOne(LPVOID lParam)
{
for(;;)
{
if (WorkerID<MAXWORKERID)
{
WorkerID +=1;
Sleep(1000);
printf("ThreadOne print out: %i \n",WorkerID);
}
}
return 0;
}
DWORD __stdcall ThreadFunTwo(LPVOID lParam)
{
for(;;)
{
if (WorkerID<MAXWORKERID)
{
WorkerID +=1;
printf("ThreadTwo print out: %i \n",WorkerID);
Sleep(1000);
}
}
return 0;
}
void main()
{
//Define thread handle
HANDLE hThread1,hThread2;
//Create thread
hThread1 = ::CreateThread(NULL,0,ThreadFunOne,NULL,0,NULL);
hThread2 = ::CreateThread(NULL,0,ThreadFunTwo,NULL,0,NULL);
//Close thread handle
CloseHandle(hThread1);
CloseHandle(hThread2);
//Note: Prevent process exiting
while (true)
{
;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?