Couchbase C Client  2.10.7
Asynchronous C Client for Couchbase
Thread Safety

The library uses no internal locking and is thus not safe to be used concurrently from multiple threads. As the library contains no globals you may call into the library from multiple threads so long as the same data structure (specifically, the same lcb_t) is not used.

#include <pthread.h>
#include <libcouchbase/api3.h>
typedef struct {
lcb_t instance;
pthread_mutex_t mutex;
} my_CTX;
/*
* This function uses the same instance between threads. A lock
* is required for every operation
*/
static void *
thrfunc_locked(void *arg)
{
my_CTX *ctx = arg;
lcb_CMDGET cmd = { 0 };
LCB_CMD_SET_KEY(&cmd, "Hello", strlen("Hello"));
pthread_mutex_lock(&ctx->mutex);
lcb_get3(ctx->instance, NULL, &cmd);
lcb_wait(ctx->instance);
pthread_mutex_unlock(&ctx->mutex);
return NULL;
}
/*
* This function uses an instance per thread. Since no other thread
* is using the instance, locking is not required
*/
static void *
thrfunc_unlocked(void *arg)
{
lcb_t instance;
lcb_create(&instance, NULL);
lcb_connect(instance);
lcb_wait(instance);
lcb_get_cmd_t cmd = { 0 }, *cmdp = &cmd;
lcb_get(instance, NULL, 1, &cmdp);
lcb_wait(instance);
lcb_destroy(instance);
return NULL;
}
int main(void)
{
pthread_t thrs[10];
my_CTX ctx;
int ii;
lcb_create(&ctx.instance, NULL);
lcb_connect(ctx.instance);
lcb_wait(ctx.instance);
pthread_mutex_init(&ctx.mutex, NULL);
for (ii = 0; ii < 10; ii++) {
pthread_create(&thrs[ii], NULL, thrfunc_locked, &ctx);
}
for (ii = 0; ii < 10; ii++) {
void *ign;
pthread_join(thrs[ii], &ign);
}
lcb_destroy(ctx.instance);
pthread_mutex_destroy(&ctx.mutex);
for (ii = 0; ii < 10; ii++) {
pthread_create(&thrs[ii], NULL, thrfunc_unlocked, NULL);
}
for (ii = 0; ii < 10; ii++) {
void *ign;
pthread_join(thrs[ii], &ign);
}
return 0;
}

In this quick mockup example, the same lcb_t is being used from multiple threads and thus requires locking. Now if each thread created its own lcb_t it would be free to operate upon it without locking.

lcb_CMDGET
Command for retrieving a single item.
Definition: couchbase.h:840
lcb_wait
lcb_error_t lcb_wait(lcb_t instance)
Wait for the execution of all batched requests.
lcb_connect
lcb_error_t lcb_connect(lcb_t instance)
Schedule the initial connection This function will schedule the initial connection for the handle.
lcb_get3
lcb_error_t lcb_get3(lcb_t instance, const void *cookie, const lcb_CMDGET *cmd)
Spool a single get operation.
LCB_CMD_SET_KEY
#define LCB_CMD_SET_KEY(cmd, keybuf, keylen)
Set the key for the command.
Definition: couchbase.h:556
lcb_t
struct lcb_st * lcb_t
Definition: couchbase.h:41
lcb_create
lcb_error_t lcb_create(lcb_t *instance, const struct lcb_create_st *options)
Create an instance of lcb.
couchbase.h
lcb_get
lcb_error_t lcb_get(lcb_t instance, const void *command_cookie, lcb_SIZE num, const lcb_get_cmd_t *const *commands)
Get a number of values from the cache.
lcb_destroy
void lcb_destroy(lcb_t instance)
Destroy (and release all allocated resources) an instance of lcb.