Multithread 之 wait

  • Sleep
    • void Sleep( DWORD dwMilliseconds );
    • This function suspends the execution of the current thread for a specified interval.
  • Busy Loop
    • 不断调用GetExitCodeThread(),直到其结果不再是STILL_ACTIVE
    • 缺点:浪费CPU时间
  • WaitForSingleObject
    • DWORD WaitForSingleObject( HANDLE hHandle, DWORD dwMilliseconds );
    • This function returns when the specified object is in the signaled state or when the time-out interval elapses.
    • Objecs:
      • Event
      • Mutex
      • Semaphore
      • Process
      • Thread :线程结束时,线程对象被激发(signaled state)。
  • WaitForMultipleObjects
    • DWORD WaitForMultipleObjects( DWORD nCount, CONST HANDLE* lpHandles, BOOL fWaitAll, DWORD dwMilliseconds );
    • This function returns a value when either any one of the specified objects is in the signaled state, or the time-out interval elapses.
  • MsgWaitForMultipleObjects
    • DWORD MsgWaitForMultipleObjects( DWORD nCount, LPHANDLE pHandles, BOOL fWaitAll, DWORD dwMilliseconds, DWORD dwWakeMask );
    • 可以同时等待消息或核心对象被激发,对GUI线程来说,可防止等待核心对象时,无法回到消息循环,界面无反应
  • 参考:《Win32多线程程序设计》
原文地址:https://www.cnblogs.com/dahai/p/2109577.html