Connection pooling abstraction


typedef struct _mongoc_client_pool_t mongoc_client_pool_t

mongoc_client_pool_t is the basis for multi-threading in the MongoDB C driver. Since mongoc_client_t structures are not thread-safe, this structure is used to retrieve a new mongoc_client_t for a given thread. This structure is thread-safe.


#include <mongoc.h>

static void *
worker (void *data)
   mongoc_client_pool_t *pool = data;
   mongoc_client_t *client;

   do {
      client = mongoc_client_pool_pop (pool);
       * Do something with client. If you are writing an HTTP server, you
       * probably only want to hold onto the client for the portion of the
       * request performing database queries.
      mongoc_client_pool_push (pool, client);
   } while (!inShutdown);

   return NULL;

int main (int argc, char *argv[])
   mongoc_client_pool_t *pool;
   mongoc_uri_t *uri;
   pthread_t thread[10];
   unsigned i;
   void *ret;

   mongoc_init ();

   uri = mongoc_uri_new ("mongodb://");
   pool = mongoc_client_pool_new (uri);

   for (i = 0; i < 10; i++) {
      pthread_create (&thread, NULL, worker, pool);

   mongoc_uri_destroy (uri);

   for (i = 0; i < 10; i++) {
      pthread_join (threads [i], &ret);

   mongoc_cleanup ();

   return 0;