Harry Boy
Harry Boy

Reputation: 4747

Is there a C++ equivalent of WaitforSingleObject?

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.

Upvotes: 10

Views: 13096

Answers (2)

no one special
no one special

Reputation: 1736

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.

Upvotes: 0

Marco A.
Marco A.

Reputation: 43662

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

Upvotes: 6

Related Questions