Jacob Garby
Jacob Garby

Reputation: 855

Use select() with a non-file-descriptor-based input

To my knowledge, the select() function in C can only wait for file descriptors to become active (i.e. for reading them to not block.)

This is useful for a command-line messaging application since everything will be either a socket file descriptor, or stdin.

However, what if I want to incorporate this with a GUI application (for example, one written in Gtk?)

I assume there's no way to tell select() to wait for a button to be pressed, right? So will I have to use multithreading?

Upvotes: 0

Views: 186

Answers (1)

rici
rici

Reputation: 241911

If you want to incorporate non-fd activity into a select-based event loop (or other fd-related alternatives like epoll), you can do that by using a pipe. The action triggered by the event (such as a button press) writes a description of the event into the pipe, and the select mask includes the read end of the pipe, so it will be notified of the data availability.

If the events and the handlers are in the same process, it's not necessary to fully serialise the event description, since some other mechanism could be used (a in-memory queue of events, or some such). However, since most events can be easily and efficiently described in a few bytes, serialising the event provides an easily scalable architecture.

Upvotes: 2

Related Questions