多线程同步用到的基本的Synchronization Object

(0) Base...
//创建线程
         for (i = 0; i < THREADCOUNT; i++)
{
aThread[i] = CreateThread(
NULL,       // default security attributes
0,          // default stack size
(LPTHREAD_START_ROUTINE)ThreadProc,
NULL,       // no thread function arguments
0,          // 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
);


(1) Semaphore Object
//创建
HANDLE ghSemaphore = CreateSemaphore(
NULL,           // default security attributes
MAX_SEM_COUNT,  // initial count
MAX_SEM_COUNT,  // maximum count
NULL);          // unnamed semaphore

//销毁关闭
         CloseHandle (ghSemaphore);

//信号量减
dwWaitResult = WaitForSingleObject(
ghSemaphore,   // handle to semaphore
0L);           // zero-second time-out interval

//信号量加
if (!ReleaseSemaphore(
ghSemaphore,  // handle to semaphore
1,            // increase count by one
NULL))       // 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 attributes
TRUE,               // manual-reset event
FALSE,              // initial state is nonsignaled
TEXT("WriteEvent")  // object name
);

//销毁Event
      CloseHandle(ghWriteEvent);

// SetEvent,使Event有信号
      SetEvent(ghWriteEvent)
// ResetEvent,使Event无信号
BOOL WINAPI ResetEvent(
  _In_  HANDLE hEvent
);
// PulseEvent
不推荐使用。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值