Let's take a glance at the recipe Creating work_queue class. Each task there can be executed in one of the many threads and we do not know in which one. Imagine that we want to send the results of an executed task using some connection:
#include <boost/noncopyable.hpp>
class connection: boost::noncopyable {
public:
// Opening a connection is a slow operation
void open();
void send_result(int result);
// Other methods
// ...
};
We have the following solutions:
- Open a new connection when we need to send the data (which is very slow)
- Have a single connection for all the threads and wrap them in mutex (which is also slow)
- Have a pool of connections, get a connection from it in a thread-safe manner, and use it (a lot of coding is required, but this solution is fast)
- Have a single connection per thread (fast and simple...