6

What is the equivalent linux API for WaitForMultipleObjects() and WSAEnumNetworkEvents()? Can we use pthread_cond_wait() for WaitForMultipleObjects?

ABC
  • 91
  • 2
  • 4
  • Possible duplicate of [WaitForSingleObject and WaitForMultipleObjects equivalent in linux](http://stackoverflow.com/questions/2719580/waitforsingleobject-and-waitformultipleobjects-equivalent-in-linux) – rogerdpack Jan 14 '16 at 17:14

2 Answers2

8

For developers coming from a Windows background, we faced the same issue when porting some code from Win32 to pthreads and created an open source (MIT-licensed) library called pevents which implements WaitForMultipleObjects() on Linux, with support for both auto and manual reset events. It should behave in the same way as WIN32 events on Windows do.

Mahmoud Al-Qudsi
  • 28,357
  • 12
  • 85
  • 125
  • 1
    sweet -- I've struggled with an implementation for a long time. – erict Dec 08 '14 at 21:50
  • 1
    Doesn't pevents realize waiting by polling? If it does, its functions are not equivalents of the `WaitForMultipleObject()` API which performs the wait by blocking. (And not eating up CPU time.) – mg30rg May 18 '15 at 11:47
  • 2
    @mg30rg I'm not sure why you would say/assume that. pevents is fully asynchronous and event-driven, using native posix synchronization primitives to replicate the behavior of the Windows' ones. It's most-decidedly *not* a polling solution. – Mahmoud Al-Qudsi May 18 '15 at 15:58
3

Well there is no straightforward API in Linux, that does the WaitForMultipleObjects() equivalent.

WaitForSingleObject and WaitForMultipleObjects equivalent in Linux? contains answer to the first part and perhaps a better explanation too.

For WSAEnumNetworkEvents(), in Linux, use poll() or select() based on your requirements. Another application libevent might also be useful.

Reference:

jww
  • 97,681
  • 90
  • 411
  • 885
askmish
  • 6,464
  • 23
  • 42
  • Thanks for the quick reply, if select() is used for this, then what should we use for WSAEventSelect()? Or should we use select for WSAEventSelect() and poll for WSAEnumNetworkEvents()?? – ABC Dec 18 '12 at 18:11
  • If I am not wrong, select() is used to register the new socket for read/write/close network events and poll() as you suggested would be used to check which network event (read/write/close) has occured..? – ABC Dec 18 '12 at 18:14
  • [This link](http://www.unixguide.net/network/socketfaq/2.14.shtml) will answer your queries better. :) – askmish Dec 19 '12 at 09:05
  • I would also suggest going through [this link](http://publib.boulder.ibm.com/infocenter/iseries/v6r1m0/index.jsp?topic=/rzab6/xnonblock.htm) to gain a better understanding with examples. If you still need help, I will be happy to help. :) – askmish Dec 19 '12 at 09:08