首页 > 代码库 > lockFunctionDemo
lockFunctionDemo
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<process.h> 4 #include<windows.h> 5 6 int g_nCount1 = 0,g_nCount2 = 0; 7 bool g_bThread = true; 8 UINT __stdcall ThreadProc(LPVOID); 9 int main(int argc,char* argv[]) 10 { 11 HANDLE h[2]; 12 unsigned m_uThreadId; 13 h[0] = (HANDLE)::_beginthreadex(NULL,0,ThreadProc,NULL,0,&m_uThreadId); 14 h[1] = (HANDLE)::_beginthreadex(NULL,0,ThreadProc,NULL,0,&m_uThreadId); 15 Sleep(1000); 16 g_bThread = false; 17 ::WaitForMultipleObjects(2,h,true,INFINITE); 18 printf("g_nCount1: %d g_nCount2: %d\n",g_nCount1,g_nCount2); 19 ::CloseHandle(h[0]); 20 ::CloseHandle(h[1]); 21 22 system("pause"); 23 return 0; 24 } 25 UINT __stdcall ThreadProc(LPVOID lpParam) 26 { 27 while(g_bThread) 28 { 29 InterlockedIncrement((long*)&g_nCount1); 30 InterlockedDecrement((long*)&g_nCount2); 31 } 32 return 0; 33 }
lockFunctionDemo
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。