Using Mongo C Driver in multi threaded application

170 views
Skip to first unread message

Meiqi Zhu

unread,
Mar 20, 2016, 5:49:15 AM3/20/16
to mongodb-user
I am using MongoDB in my multi-threaded application written in 'C'.  Ecah thread hold a client created by mongoc_client_pool_pop to write and read.
In most cases , My Code works and can get the right result. But sometimes I get errors like:

           Failed to decode reply BSON document.
           Failed to send "ping" command with database "admin": Failure during socket delivery: Invalid argument (22)

i am wondering wheather the mongo c driver is thread safe  or not ? 

Wan Bachtiar

unread,
Apr 6, 2016, 2:36:34 AM4/6/16
to mongodb-user

i am wondering wheather the mongo c driver is thread safe or not ?

Hi Meiqi,

As mentioned in the API manual for MongoDB C Driver v1.3.x: Threading, the driver is thread unaware for most of its operations. It is up to the you to guarantee thread-safety. You could use the thread-safe mongoc_client_pool_t to retrieve a mongo_client_t for your thread.

Each thread hold a client created by mongoc_client_pool_pop to write and read.

If you are already using mongoc_client_pool_t, make sure that each mongo_client_t retrieved is being used only for that given thread.

Regards,

Wan.

Reply all
Reply to author
Forward
0 new messages