MONGOC_CLIENT_POOL_T(3)

πŸ’… MONGOC_CLIENT_POOL_T(3) ✨

okay bestie gather round, because this is the libmongoc page that makes all your multithreaded dreams come true 🫢 it's a connection pool for multi-threaded programs, and if you want the full lore you gotta see Connection Pooling too. no cap.


✨ SYNOPSIS ✨

typedef struct _mongoc_client_pool_t mongoc_client_pool_t

🌸 DESCRIPTION 🌸

so mongoc_client_pool_t is literally the blueprint for multi-threading in the MongoDB C driver πŸ‘‘ like, the whole foundation, the main character of concurrency.

here's the tea πŸ“: mongoc_client_t structures are not thread-safe. not even a little. so instead of sharing one around like a group-chat screenshot, you use this pool structure to retrieve a new mongoc_client_t for a given thread. one client per thread, everyone gets their own, everyone slays πŸ’….

and mongoc_client_pool_t itself? thread-safe ✨ iconic. except for its destructor method, mongoc_client_pool_destroy(), which is not thread-safe.

πŸ’€ real talk: mongoc_client_pool_destroy() must only be called from one thread. one. don't be cute about it, don't have three threads trying to destroy the pool at the same time, that's not the vibe check you want to fail, fr fr.

πŸŽ€ EXAMPLE πŸŽ€

it's giving example-pool.c πŸ¦‹:

/* gcc example-pool.c -o example-pool $(pkg-config --cflags --libs
 * libmongoc-1.0) */

/* ./example-pool [CONNECTION_STRING] */

#include <mongoc/mongoc.h>

#include <pthread.h>

#include <stdio.h>

static pthread_mutex_t mutex;
static bool in_shutdown = false;

static void *
worker(void *data)
{
   mongoc_client_pool_t *pool = data;
   mongoc_client_t *client;
   bson_t ping = BSON_INITIALIZER;
   bson_error_t error;
   bool r;

   BSON_APPEND_INT32(&ping, "ping", 1);

   while (true) {
      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.
       */
      r = mongoc_client_command_simple(client, "admin", &ping, NULL, NULL, &error);

      if (!r) {
         fprintf(stderr, "%s\n", error.message);
      }

      mongoc_client_pool_push(pool, client);

      pthread_mutex_lock(&mutex);
      if (in_shutdown || !r) {
         pthread_mutex_unlock(&mutex);
         break;
      }

      pthread_mutex_unlock(&mutex);
   }

   bson_destroy(&ping);
   return NULL;
}

int
main(int argc, char *argv[])
{
   const char *uri_string = "mongodb://127.0.0.1/?appname=pool-example";
   mongoc_uri_t *uri;
   bson_error_t error;
   mongoc_client_pool_t *pool;
   pthread_t threads[10];
   unsigned i;
   void *ret;

   pthread_mutex_init(&mutex, NULL);
   mongoc_init();

   if (argc > 1) {
      uri_string = argv[1];
   }

   uri = mongoc_uri_new_with_error(uri_string, &error);
   if (!uri) {
      fprintf(stderr,
              "failed to parse URI:\n"
              "error message: %s\n",
              error.message);
      return EXIT_FAILURE;
   }

   pool = mongoc_client_pool_new(uri);
   mongoc_client_pool_set_error_api(pool, 2);

   for (i = 0; i < 10; i++) {
      pthread_create(&threads[i], NULL, worker, pool);
   }

   sleep(10);
   pthread_mutex_lock(&mutex);
   in_shutdown = true;
   pthread_mutex_unlock(&mutex);

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

   mongoc_client_pool_destroy(pool);
   mongoc_uri_destroy(uri);

   mongoc_cleanup();

   return EXIT_SUCCESS;
}

ten whole threads, all popping clients and pushing them back like it's a lending library πŸ“š the way this example eats, honestly πŸ”₯


πŸ’œ AUTHOR πŸ’œ

MongoDB, Inc β€” the icons themselves πŸ‘‘


2009-present, MongoDB, Inc.


and that's the whole page bestie, you ate that reading πŸ§šπŸ’– this one's from version 2.5.6, dated Oct 09, 2026 β€” go forth and pool responsibly! 🌈⭐

πŸ“¦ where this page comes from