10

I need to rewrite some code that uses the windows WaitforSingleObject function.

myEvent = CreateEvent( NULL, FALSE, FALSE, szName );
WaitForSingleObject( myEvent, nMilliseconds );

I need to wait for an event or for a timeout to happen. Is there an equivalent to this in straight C++??

I am using STL C++11 and not any other libraries such as boost.

Harry Boy
  • 4,159
  • 17
  • 71
  • 122

2 Answers2

6

You can't use C++11 thread routines with win32 threads (unless you heavily mess up with mingw thread implementations, something I would not recommend) and there's no standard C++ equivalent to an OS-specific API call.

You can, however use C++11 threads and use condition variables (cfr. waiting) to accomplish the same thing that WaitForSingleObject does, i.e.

  • Wait for an object to be in a signaled state
  • Wait until a timeout elapses

Edit: specifically you would need wait_until

Marco A.
  • 43,032
  • 26
  • 132
  • 246
  • 2
    The equivalent of the API call would be `wait_for`, not `wait_until`. – D Drmmr Sep 26 '14 at 11:15
  • 2
    Can Visual Studio's `` be safely mixed with winapi threads? – Siyuan Ren Sep 26 '14 at 11:16
  • 2
    "mess up with mingw thread" - this is not just a mingw thing. VS also implements the `` library and equally, mixing Win32 and C++ threads can cause adverse issues - they are not automatically compatible. IIRC, VS uses the ConcRT to implement the C++ threading components. – Niall Sep 26 '14 at 11:21
  • 2
    Some mingw builds do use windows thread model, can't remember which ones but it might be pretty common anyway – Marco A. Sep 26 '14 at 11:22
-1

Yes, you can use C++11 Only. Here is an example (too long to be pasted here): https://github.com/moya-lang/Event/blob/master/Event.h. The code fully mimics WINAPI Event Objects so this is something you are asking for.

no one special
  • 1,608
  • 13
  • 32