Many users ask us how Mongoose Embedded Web Server can be used in a multi-threaded environment. It is not trivial, and today we’ll show you how to do it correctly.
Mongoose manager is single-threaded. It does not perform any kind of synchronisation internally and it is the responsibility of the user to ensure that every state - connections and the manager itself - is only modified from one thread.
A simple way of ensuring this is to only ever touch the state in a handler when it is invoked during mg_mgr_poll. But, it is not always practical. For example, when the handler needs to perform a potentially blocking operation.
While not enabled by default, Mongoose does contain a helper that can be used to decouple incoming connection handling from the mg_mgr_poll loop. It covers the most common case of a server handling user requests. The example doesn’t look much different, the main difference being calling the mg_enabled_multithreading function on the listening connection.
Under the hood mg_enabled_multithreading works by launching a connection handling thread for each new accepted connection (thus implementing a thread per connection, TPC scheme) and introducing an additional pair of sockets to synchronise between the handler thread and the main polling thread. It may add a bit of latency to processing, but will come handy if the operations that the handler performs are blocking and time consuming.