标签:
// windows_37_Thread_InterLock.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "windows.h"long g_nValue1 = 0;long g_nValue2 = 0;DWORD WINAPI InterProc1( LPVOID pParam ){for (int nIndex = 0; nIndex < 10000000;nIndex++){//普通的加加g_nValue1++;}return 0;}DWORD WINAPI InterProc2( LPVOID pParam ){for (int nIndex = 0; nIndex < 100;nIndex++){//原子锁加加//g_nValue2++;InterlockedIncrement( &g_nValue2 );}return 0;}void Create( ){DWORD nThreadID = 0;HANDLE hThread[4] = { NULL };hThread[0] = CreateThread( NULL, 0, InterProc1, NULL, 0, &nThreadID );hThread[1] = CreateThread( NULL, 0, InterProc1, NULL, 0, &nThreadID );hThread[2] = CreateThread( NULL, 0, InterProc2, NULL, 0, &nThreadID );hThread[3] = CreateThread( NULL, 0, InterProc2, NULL, 0, &nThreadID );WaitForMultipleObjects( 4, hThread, FALSE, INFINITE );printf( "%ld\n", g_nValue1 );printf( "%ld\n", g_nValue2 );}int _tmain(int argc, _TCHAR* argv[]){Create( );return 0;}
37 windows_37_Thread_InterLock 线程-原子锁
标签:
原文地址:http://www.cnblogs.com/nfking/p/5573918.html