| 1 |
# Multithreading {#mt_page} |
| 2 |
|
| 3 |
Full multithreading support is available with C++11 and later. Limited |
| 4 |
multithreading is possible with older versions of C++. See the last |
| 5 |
section of this page for more information. |
| 6 |
|
| 7 |
`proton::container` handles multiple connections concurrently in a |
| 8 |
thread pool, created using `proton::container::run()`. As AMQP events |
| 9 |
occur on a connection the container calls `proton::messaging_handler` |
| 10 |
event callbacks. The calls for each connection are *serialized* - |
| 11 |
callbacks for the same connection are never made concurrently. |
| 12 |
|
| 13 |
You assign a handler to a connection in `proton::container::connect()` |
| 14 |
or `proton::listen_handler::on_accept()` with |
| 15 |
`proton::connection_options::handler()`. We recommend you create a |
| 16 |
separate handler for each connection. That means the handler doesn't |
| 17 |
need locks or other synchronization to protect it against concurrent |
| 18 |
use by Proton threads. If you use the handler concurrently from |
| 19 |
non-Proton threads then you will need synchronization. |
| 20 |
|
| 21 |
The examples @ref multithreaded_client.cpp and @ref |
| 22 |
multithreaded_client_flow_control.cpp illustrate these points. |
| 23 |
|
| 24 |
## Thread-safety rules |
| 25 |
|
| 26 |
`proton::container` is thread-safe *with C++11 or greater*. An |
| 27 |
application thread can open (or listen for) new connections at any |
| 28 |
time. The container uses threads that call `proton::container::run()` |
| 29 |
to handle network IO and call user-defined `proton::messaging_handler` |
| 30 |
callbacks. |
| 31 |
|
| 32 |
`proton::container` ensures that calls to event callbacks for each |
| 33 |
connection instance are *serialized* (not called concurrently), but |
| 34 |
callbacks for different connections can be safely executed in |
| 35 |
parallel. |
| 36 |
|
| 37 |
`proton::connection` and related objects (`proton::session`, |
| 38 |
`proton::sender`, `proton::receiver`, `proton::delivery`) are *not* |
| 39 |
thread-safe and are subject to the following rules. |
| 40 |
|
| 41 |
1. They can only be used from a `proton::messaging_handler` event |
| 42 |
callback called by Proton or a `proton::work_queue` function (more |
| 43 |
below). |
| 44 |
|
| 45 |
2. You cannot use objects belonging to one connection from a callback |
| 46 |
for another connection. We recommend a single handler instance per |
| 47 |
connection to avoid confusion. |
| 48 |
|
| 49 |
3. You can store Proton objects in member variables for use in a later |
| 50 |
callback, provided you respect rule two. |
| 51 |
|
| 52 |
`proton::message` is a value type with the same threading constraints |
| 53 |
as a standard C++ built-in type. It cannot be concurrently modified. |
| 54 |
|
| 55 |
## Work queues |
| 56 |
|
| 57 |
`proton::work_queue` provides a safe way to communicate between |
| 58 |
different connection handlers or between non-Proton threads and |
| 59 |
connection handlers. |
| 60 |
|
| 61 |
* Each connection has an associated `proton::work_queue`. |
| 62 |
|
| 63 |
* The work queue is thread-safe (C++11 or greater). Any thread can |
| 64 |
add *work*. |
| 65 |
|
| 66 |
* *Work* is a `std::function`, and bound arguments will be |
| 67 |
called like an event callback. |
| 68 |
|
| 69 |
When the work function is called by Proton, it will be serialized |
| 70 |
safely so that you can treat the work function like an event callback |
| 71 |
and safely access the handler and Proton objects stored on it. |
| 72 |
|
| 73 |
The examples @ref multithreaded_client.cpp and @ref |
| 74 |
multithreaded_client_flow_control.cpp show how you can send and |
| 75 |
receive messages from non-Proton threads using work queues. |
| 76 |
|
| 77 |
## The wake primitive |
| 78 |
|
| 79 |
`proton::connection::wake()` allows any thread to "wake up" a |
| 80 |
connection by generating an `on_connection_wake()` callback. This is |
| 81 |
the *only* thread-safe `proton::connection` function. |
| 82 |
|
| 83 |
This is a lightweight, low-level primitive for signaling between |
| 84 |
threads. |
| 85 |
|
| 86 |
* It does not carry any code or data (unlike `proton::work_queue`). |
| 87 |
|
| 88 |
* Multiple calls to `wake()` can be "coalesced" into a single |
| 89 |
`on_connection_wake()`. |
| 90 |
|
| 91 |
* Calls to `on_connection_wake()` can occur without any call to |
| 92 |
`connection::wake()`. Proton uses wake internally. |
| 93 |
|
| 94 |
The semantics of `wake()` are similar to |
| 95 |
`std::condition_variable::notify_one`. There will be a wakeup, but |
| 96 |
there must be some shared application state to determine why the |
| 97 |
wakeup occurred and what, if anything, to do about it. |
| 98 |
|
| 99 |
Work queues are easier to use in many instances, but `wake()` may be |
| 100 |
useful if you already have your own external thread-safe queues and |
| 101 |
just need an efficient way to wake a connection to check them for |
| 102 |
data. |
| 103 |
|
| 104 |
## Using older versions of C++ |
| 105 |
|
| 106 |
Before C++11 there was no standard support for threading in C++. You |
| 107 |
can use Proton with threads but with the following limitations. |
| 108 |
|
| 109 |
* The container will not create threads, it will only use the single |
| 110 |
thread that calls `proton::container::run()`. |
| 111 |
|
| 112 |
* None of the Proton library classes are thread-safe, including |
| 113 |
`proton::container` and `proton::work_queue`. You need an external |
| 114 |
lock to use `proton::container` in multiple threads. |
| 115 |
|
| 116 |
The only exception is `proton::connection::wake()`, it *is* |
| 117 |
thread-safe even in older C++. |
| 118 |
|
| 119 |
You can implement your own `proton::container` using your own |
| 120 |
threading library, or ignore the container and embed the lower-level |
| 121 |
`proton::io::connection_driver` in an external poller. These |
| 122 |
approaches still use the same `proton::messaging_handler` callbacks, |
| 123 |
so you can reuse most of your application code. Note that this is an |
| 124 |
advanced undertaking. There are a few pointers in @ref io_page. |