## Event Loop and Thread Pool interaction |
|
The CppCMS service has a simple design of a single event |
loop and a thread pool that handles actual user |
loop, and a thread pool that handles actual user |
application responses. |
|
Every new incoming FastCGI, SCGI or HTTP request |
is accepted by the CppCMS service and handled |
by the fast and efficient CppCMS event loop that is |
based on `epoll`, `kqueue`, `/dev/poll` or `select` |
system calls. |
|
On an incoming request, a special HTTP Context is created. |
It prepares the incoming request, reads all POST data and |
if required parses it within the central event loop. |
|
Once the request is ready, it is checked against the |
CppCMS applications pool to detect which |
application object handles it. |
|
If the application that handles it is a synchronous |
application, its execution is passed to the |
_thread pool_. The user application prepares the response |
and sends it synchronously to the client and the context |
is destroyed, completing the "requst/response" cycle. |
is destroyed, completing the "request/response" cycle. |
|
If the application is asynchronous, the HTTP Context |
remains inside the event loop's thread and it is handled by |
the asynchronous application. This application |
may decide to complete the response immediately |
or postpone it by implementing, for example, long polling. |
|
|
## Integration with CppCMS Event Loop |
|
There are several ways to interact with the event loop, |
besides the obvious - implementing your own asynchronous |
application: |
|
1. Use the `cppcms::service::get_io_service()` function |
to access the event loop directly. |
|
You can use any of the [`booster::aio`](/cppcms_ref_v0_99/namespacebooster_1_1aio.html) classes there |
and handle various asynchronous events. |
|
2. You can post various operations for execution |
in the event loop using `cppcms::service::post()` |
from other threads. |
|
This is especially important when you want to |
do integration between synchronous and asynchronous |
applications. |
|
You should remember that most of the CppCMS objects |
including `http::context` are not thread safe |
for access from multiple threads, so |
if some operation should be executed on |
objects running in the event loop, it must |
be done by posting handlers for execution |
in the event loop. |
|
It is important to remember that |
every operation executed in the event |
loop should be _very fast_ and _never block_. |
|
|