MongoDB C Client Library / Threaded (libmongoc)

67 views
Skip to first unread message

J-EC

unread,
Apr 4, 2018, 9:16:42 PM4/4/18
to mongodb-user
Hello,

While reading the Mongo C Client library documentation regarding threading & connection pooling (mongoc_client_pool_t), it seems clear that an application using this library will likely need to be multi-threaded for best performance.  I understand there won't be a single-threaded async client as discussed in this "won't fix" JIRA:

https://jira.mongodb.org/browse/CDRIVER-27

I'm thinking that my application will need to implement a thread pool + producer / consumer model to interwork with the Mongo C Client library.  But I'd like to see how other open-source apps make use of the C Client library to confirm that this is the right track, and to hopefully avoid pitfalls.  I've searched and searched and come up empty. 

Anyone know of open-source apps that use the Mongo C Client library?

Thanks!

A. Jesse Jiryu Davis

unread,
Apr 5, 2018, 4:30:25 PM4/5/18
to mongodb-user
Hi, there's no need for anything as complex as you're suggesting. Here's an example that uses a mongocxx::pool simultaneously in 3 threads:


Just make sure that each mongocxx::client is used by only one thread at a time. The same rule applies to any database, collection, or cursor object you create from that client.

A. Jesse Jiryu Davis

unread,
Apr 5, 2018, 4:33:41 PM4/5/18
to mongodb-user
Oh I'm sorry, you asked about C, not C++. For C, here's an example that uses a mongoc_client_pool_t with 10 threads at a time for 10 seconds:


The basic idea is the same again: mongoc_client_pool_t is thread-safe, but mongoc_client_t is not, and you must use the client and any database, collection, or cursor you create from the client all in the same thread.
Reply all
Reply to author
Forward
0 new messages