I have been looking into event driven programming in C and the language doesn't seem to have support for it. This has led me to wonder about the nature of event driven programming. Is it correct to say that an event handler basically spawns a thread that waits for a response from I/O and sets a variable? Or is there something more complicated to the process.
|
Event driven architecture simply means, that the main loop AKA event loop does not understand much about the data a program processes. It understands only enough, that it can dispatch the right events for the data. So for example, if event is triggered by data coming from a socket, all the event loop needs to know is, what callback to call, when data comes from that particular socket. Typically the event loop is in some library, such as libevent. So application code consists of just setup code and event handlers which are called by the event loop in the library. Since you mention threads: a classic simple event driven application is single threaded. Each event handler or callback just does its thing as fast as it can, when it gets called, and returns. Blocking or waiting or sleeping is a big no no, it will block all other events! Threads are often used, when there is some processing which takes long time. If it were done in the main thread with event loop, the long operation would have to be chopped into small pieces, so other events could be handled too. So it's simpler to have a worker thread, which can do the processing without interfering with event processing, and then generate event for main event loop when done. Other case of using threads is, when there is something which must block. It may for example be some other library, which only provides a blocking API. If that API was to be used from the main thread event loop, it would block other events. Solution is to use that library from another thread, which can block, and then generate event for the main event loop when there's something interesting for other parts of the application. But in a sense, event driven architecture is sort of opposite of multi-threaded architecture:
Of course a complex application can have several event loops in different threads, and also threads which do just single thing, so these can be mixed. |
|||||||||||||||||
|