从品牌网站建设到网络营销策划,从策略到执行的一站式服务
SRWLOCK g_lock = {}; //轻量级锁 CONDITION_VARIABLE g_val={};//轻量级锁的同步条件 INT iGlobal = 0; //读写的全局变量 DWORD WINAPI WriteThread(LPVOID lpParameter) {//写线程 while (1) { Sleep(20); __try { AcquireSRWLockExclusive(&g_lock);//申请独占锁 iGlobal++; } __finally { ReleaseSRWLockExclusive(&g_lock);//释放独占锁 WakeAllConditionVariable(&g_val);//唤醒条件变量 } } return 0; } DWORD WINAPI ReadThread(LPVOID lpParameter) {//读线程 while (true) { Sleep(30); __try { AcquireSRWLockShared(&g_lock);//申请共享锁 BOOL rt = SleepConditionVariableSRW(&g_val, &g_lock, 20, CONDITION_VARIABLE_LOCKMODE_SHARED);//等待条件变量 if (rt) { char str[20] = {}; itoa(iGlobal, str, 10); TRACE(str); } else { char str[20] = {}; itoa(iGlobal, str, 10); TRACE(str); } } __finally { ReleaseSRWLockShared(&g_lock);//释放锁 } } return 0; } void CtestThreadDlg::OnBnClickedButton15() { InitializeSRWLock(&g_lock); InitializeConditionVariable(&g_val); DWORD ThreadID =0; HANDLE hWrite = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)WriteThread, NULL, CREATE_SUSPENDED, &ThreadID); HANDLE hRead = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ReadThread, NULL, CREATE_SUSPENDED, &ThreadID); ResumeThread(hWrite); ResumeThread(hRead); }
成都网站建设公司地址:成都市青羊区太升南路288号锦天国际A座10层 建设咨询028-86922220
成都快上网科技有限公司-四川网站建设设计公司 | 蜀ICP备19037934号 Copyright 2020,ALL Rights Reserved cdkjz.cn | 成都网站建设 | © Copyright 2020版权所有.
专家团队为您提供成都网站建设,成都网站设计,成都品牌网站设计,成都营销型网站制作等服务,成都建网站就找快上网! | 成都网站建设哪家好? | 网站建设地图