(0) Base...
//创建线程
for (i = 0; i < THREADCOUNT; i++)
{aThread[i] = CreateThread(NULL, // default security attributes0, // default stack size(LPTHREAD_START_ROUTINE)ThreadProc,NULL, // no thread function arguments0, // default creation flags&ThreadID); // receive thread identifier
if (aThread[i] == NULL){printf("CreateThread error: %d\n", GetLastError());return 1;}}
//销毁线程
for (i = 0; i < THREADCOUNT; i++)
CloseHandle(aThread[i]);
//WaitForMultipleObjects
DWORD WINAPI WaitForMultipleObjects(_In_ DWORD nCount,_In_ const HANDLE *lpHandles,_In_ BOOL bWaitAll,_In_ DWORD dwMilliseconds);
dwEvent = WaitForMultipleObjects(
2, // number of objects in array
ghEvents, // array of objects
FALSE, // wait for any object
5000); // five-second wait
// The return value indicates which event is signaled
switch (dwEvent)
{
// ghEvents[0] was signaled
case WAIT_OBJECT_0 + 0:
// TODO: Perform tasks required by this event
printf("First event was signaled.\n");
break;
// ghEvents[1] was signaled
case WAIT_OBJECT_0 + 1:
// TODO: Perform tasks required by this event
printf("Second event was signaled.\n");
break;
case WAIT_TIMEOUT:
printf("Wait timed out.\n");
break;
// Return value is invalid.
default:
printf("Wait error: %d\n", GetLastError());
ExitProcess(0);
}
//WaitForSingleObject
DWORD WINAPI WaitForSingleObject(_In_ HANDLE hHandle,_In_ DWORD dwMilliseconds);
//创建
HANDLE ghSemaphore = CreateSemaphore(NULL, // default security attributesMAX_SEM_COUNT, // initial countMAX_SEM_COUNT, // maximum countNULL); // unnamed semaphore
//销毁关闭
CloseHandle
(ghSemaphore);
//信号量减
dwWaitResult = WaitForSingleObject(ghSemaphore, // handle to semaphore0L); // zero-second time-out interval
//信号量加
if (!ReleaseSemaphore(
ghSemaphore, // handle to semaphore1, // increase count by oneNULL)) // not interested in previous count
{printf("ReleaseSemaphore error: %d\n", GetLastError());}
(2) Mutex
//CreateMutex
ghMutex = CreateMutex(
NULL, // default security attributes
FALSE, // initially not owned
NULL); // unnamed mutex
//销毁Mutex
CloseHandle(ghMutex);
//获取Mutex
dwWaitResult = WaitForSingleObject(
ghMutex, // handle to mutex
INFINITE); // no time-out interval
//释放Mutex
ReleaseMutex(ghMutex)
// OpenMutex
HANDLE WINAPI OpenMutex(_In_ DWORD dwDesiredAccess,_In_ BOOL bInheritHandle,_In_ LPCTSTR lpName);
If a named mutex does not exist, the function fails and GetLastError returns ERROR_FILE_NOT_FOUND.
CreateMutex opens a mutex if it exists and creates it if it does not.
(3) CRITICAL_SECTION
CRITICAL_SECTION m_CriticalSection;
InitializeCriticalSection(&m_CriticalSection);
DeleteCriticalSection(&m_CriticalSection);
EnterCriticalSection(&m_CriticalSection);
LeaveCriticalSection(&m_CriticalSection);
(4) Event
//CreateEvent
HANDLE ghWriteEvent = CreateEvent(
NULL, // default security attributesTRUE, // manual-reset eventFALSE, // initial state is nonsignaledTEXT("WriteEvent") // object name);
//销毁Event
CloseHandle(ghWriteEvent);
// SetEvent,使Event有信号
SetEvent(ghWriteEvent)
// ResetEvent,使Event无信号
BOOL WINAPI ResetEvent(_In_ HANDLE hEvent);
// PulseEvent
不推荐使用。