第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

剛開始學(xué),怎么也搞不懂了,求幫助啊~~~以下是用臨界區(qū)對象做的,如何用互斥對象來完成?

剛開始學(xué),怎么也搞不懂了,求幫助啊~~~以下是用臨界區(qū)對象做的,如何用互斥對象來完成?

C++
元芳怎么了 2023-03-02 16:18:42
// Mutex.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "Mutex.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// The one and only application objectCWinApp theApp;using namespace std;static int count=5;static HANDLE h1;static HANDLE h2;LPCRITICAL_SECTION hCriticalSection;CRITICAL_SECTION Critical;void func1();void func2();int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]){int nRetCode = 0;DWORD dwThreadID1,dwThreadID2;hCriticalSection=&Critical;InitializeCriticalSection(hCriticalSection);h1=CreateThread((LPSECURITY_ATTRIBUTES)NULL,0,(LPTHREAD_START_ROUTINE)func1,(LPVOID)NULL,0,&dwThreadID1);if(h1==NULL) printf("Thread1 create Fail!\n");elseprintf("Thread1 create Success!\n");h2=CreateThread((LPSECURITY_ATTRIBUTES)NULL,0,(LPTHREAD_START_ROUTINE)func2,(LPVOID)NULL,0,&dwThreadID2);if(h1==NULL) printf("Thread2 create Fail!\n");elseprintf("Thread2 create Success!\n");Sleep(1000);CloseHandle(h1);CloseHandle(h2);DeleteCriticalSection(hCriticalSection);ExitThread(0);return nRetCode;}void func2(){int r2;EnterCriticalSection(hCriticalSection);r2=count;_sleep(100);r2=r2+1;count=r2;printf("count in func2=%d\n",count);LeaveCriticalSection(hCriticalSection);}void func1(){int r1;EnterCriticalSection(hCriticalSection);r1=count;_sleep(500);r1=r1+1;count=r1;printf("count in func1=%d\n",count);LeaveCriticalSection(hCriticalSection);}
查看完整描述

1 回答

?
HUH函數(shù)

TA貢獻1836條經(jīng)驗 獲得超4個贊

你的程序也太亂了吧。
Mutex的用法也很簡單,在main函數(shù)里創(chuàng)建一個Mutex,在線程里進入一段代碼前用WaitForSIngleObject,退出代碼后用ReleaseMutex就行了。
下面是從static int count=5開始的程序,前面的不用改。
static int count=5;
static HANDLE h1;
static HANDLE h2;
HANDLE g_hMutex;
void func1();
void func2();
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
DWORD dwThreadID1,dwThreadID2;
g_hMutex = CreateMutex(NULL, FALSE, NULL);
h1=CreateThread((LPSECURITY_ATTRIBUTES)NULL,
0,
(LPTHREAD_START_ROUTINE)func1,
(LPVOID)NULL,
0,&dwThreadID1);
if(h1==NULL) 
printf("Thread1 create Fail!\n");
else
printf("Thread1 create Success!\n");
h2=CreateThread((LPSECURITY_ATTRIBUTES)NULL,
0,
(LPTHREAD_START_ROUTINE)func2,
(LPVOID)NULL,
0,&dwThreadID2);
if(h1==NULL) 
printf("Thread2 create Fail!\n");
else
printf("Thread2 create Success!\n");
Sleep(1000);
CloseHandle(h1);
CloseHandle(h2);
ExitThread(0);
return nRetCode;
}
void func2()
{
int r2;
WaitForSingleObject(g_hMutex, INFINITE);
r2=count;
_sleep(100);
r2=r2+1;
count=r2;
printf("count in func2=%d\n",count);
ReleaseMutex(g_hMutex);
}
void func1()
{
int r1;
WaitForSingleObject(g_hMutex, INFINITE);
r1=count;
_sleep(500);
r1=r1+1;
count=r1;
printf("count in func1=%d\n",count);
ReleaseMutex(g_hMutex);
}


查看完整回答
反對 回復(fù) 2023-03-06
  • 1 回答
  • 0 關(guān)注
  • 70 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號