当前位置:网站首页>MFC multithreaded semaphore csemaphore critical area and mutually exclusive events

MFC multithreaded semaphore csemaphore critical area and mutually exclusive events

2022-06-24 07:26:00 Little yellow man software

CMutex, CCriticalSection For mutually exclusive access to resources .

CMutex Can be used across processes , CCriticalSection It can only be used inside the process .

establish CMutex Need more resources , It is only used inside the process CCriticalSection Get more efficiency . 


CCriticalSection  m_CritSection;// For critical zone   Method 1 
CSingleLock singleLock(&m_CritSection); //

//CMutex Mutex;// For mutexes   Method 2 
//CSingleLock singleLock(&Mutex); //
int StartMainLock()
{
	singleLock.Lock();// Lock yourself without being called , Have been called and wait 
	if (singleLock.IsLocked())
	{
		int ret=StartMain();  // Locked   Deal with content 
		singleLock.Unlock();// Unlock    All unlocked before returning 
		return ret;
	}
	singleLock.Unlock();// Unlock 
	return -1;
}
	// event 
	HANDLE	m_hEvent= CreateEvent(NULL, FALSE, FALSE, NULL); // newly build 
	WaitForSingleObject(m_hEvent, INFINITE); // Infinite time waiting 
	ResetEvent(m_hEvent);  Reset   For no signal 
	SetEvent(m_hEvent); // Set with signal 					
	CloseHandle(m_hEvent); // close 

CSemaphore Specifies the count of threads for the resource

原网站

版权声明
本文为[Little yellow man software]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240108030304.html