Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

459
Visualizações
MongoDB C++: Is mongocxx::pool thread safe?

Do I have to manually lock mongocxx::pool while acquiring a connection?

i.e Is this safe? (example copied from Mongo website)

mongocxx::instance instance{};
mongocxx::pool pool {mongocxx::uri{}};

using mongocxx::pool::entry = std::unique_ptr<client, std::function<void (client*)>>

auto threadfunc = [](mongocxx::client &client, stdx::string_view dbname) {
    client[dbname]["col"].insert({});
}
// don't even bother sharing clients. Just give each thread its own,
std::thread([]() {
    // pool.acquire() returns a mongo::pool::entry type
    mongocxx::client *c= pool.acquire().get();
    threadfunc(*c, "db1");
    threadfunc(*c, "db2");
});

std::thread([]() {
    mongocxx::client *c = pool.acquire().get();;
    threadfunc(*c, "db2");
    threadfunc(*c, "db1");
});
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Yes, mongocxx::pool is thread-safe. You may access it concurrently from multiple threads. However, the individual mongocxx::client objects returned from the pool are not thread-safe, nor are the subordinate objects like collection or database obtained from the client - you must not share them between threads.

Note also that your example (which is not copied verbatim from the website but is modified from one of the examples), contains a serious programming error.

This line:

   mongocxx::client *c= pool.acquire().get();

Will obtain a pool entry, then extracts a bare pointer from it. However, the pool entry will be destroyed at the end of the statement, causing the underlying client object to be returned to the pool, allowing another thread to potentially pick it up while you continue to use it.

You should write this as:

mongocxx::instance instance{};
mongocxx::pool pool {mongocxx::uri{}};

auto threadfunc = [](mongocxx::client &client, stdx::string_view dbname) {
    client[dbname]["col"].insert({});
}
// don't even bother sharing clients. Just give each thread its own,
std::thread([]() {
    // pool.acquire() returns a mongo::pool::entry type
    auto c = pool.acquire();
    threadfunc(*c, "db1");
    threadfunc(*c, "db2");
});

std::thread([]() {
    auto c = pool.acquire();
    threadfunc(*c, "db2");
    threadfunc(*c, "db1");
});

That way, each thread retains the pool entry until it is finished using it, at which point it will be automatically returned when the unique_ptr in c is destroyed.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda