I'm poring a code written (by me) for Windows to other platforms.
I have a generic infrastructure for single-threaded cooperative multitasking, and I try to make it work using POSIX API. This infrastructure allows to wait for any of the specified "events" and handle any of them appropriately. Those "events" include network (sockets), timers, and things induced by the application itself from another thread, such as "event" about queued work item that should be executed in this thread, abort request and etc.
In Windows the natural way to go is using "waitable handles" (events, semaphores and etc.) and WaitForMultipleObjects
or equivalent wait function. Sockets can be associated with events as well (WSAEventSelect
).
I wonder how I can achieve this using POSIX. What I need is a sort of a wait function, that can be given multiple events/conditions to wait for, including timeout.
Since I need (among other things) sockets, I decided to start from file/socket-specific wait functions, such as select
, poll
, epoll
. And this works as expeced.
Now I wonder how to add "internal" events. One solution I have in mind is creating "artificial" sockets for this viasocketpair
, and then send "dummy data" to induce an event in the target thread.
This however seems an overkill and ineffective. Creating sockets seems to be a complex operation. Whereas the only thing that I need is to "wake" a thread.
Any ideas?