Another Problem: State Management
This lesson discusses the challenge of state management that arises because of the event-based approach for concurrency.
We'll cover the following
Another issue with the event-based approach is that such code is generally more complicated to write than traditional thread-based code. The reason is that when an event handler issues an asynchronous I/O, it must package up some program state for the next event handler to use when the I/O finally completes. This additional work is not needed in thread-based programs, as the state the program needs is on the stack of the thread.
An example
To make this point more concrete, let’s look at a simple example in which a thread-based server needs to read from a file descriptor (fd
) and, once complete, write the data that it read from the file to a network socket descriptor (sd
). The code (ignoring error checking) looks like this:
Get hands-on with 1200+ tech skills courses.