标签:
// windows_38_thread_CriticalSection.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "windows.h"
CRITICAL_SECTION g_cs = { 0 };
long g_nValue = 0;
void Print( )
{
//进入临界区 - 加锁
EnterCriticalSection( &g_cs );
for (int i = 0; i < 10000000;i++)
{
g_nValue++;
}
printf( "Long long long ...Long long%ld\n",g_nValue );
//离开临界区 - 解锁
LeaveCriticalSection( &g_cs );
}
DWORD WINAPI PrintProc( LPVOID pWparam )
{
int i = 0;
Print( );
Sleep( 1 );
return 0;
}
void Create( )
{
DWORD nThreadID = 0;
HANDLE hThread[2] = { 0 };
hThread[0] = CreateThread( NULL, 0, PrintProc, NULL, 0, &nThreadID );
hThread[1] = CreateThread( NULL, 0, PrintProc, NULL, 0, &nThreadID );
getchar( );
}
int _tmain(int argc, _TCHAR* argv[])
{
//初始化临界区
InitializeCriticalSection( &g_cs );
Create( );
//删除临界区
DeleteCriticalSection( &g_cs );
return 0;
}
38 windows_38_thread_CriticalSection 线程-临界区
标签:
原文地址:http://www.cnblogs.com/nfking/p/5573919.html