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_INSTANCE *
) is not used.
#include <pthread.h>
typedef struct {
pthread_mutex_t mutex;
} my_CTX;
static void *
thrfunc_locked(void *arg)
{
my_CTX *ctx = arg;
lcb_CMDGET cmd = { 0 };
pthread_mutex_lock(&ctx->mutex);
lcb_get3(ctx->instance, NULL, &cmd);
pthread_mutex_unlock(&ctx->mutex);
return NULL;
}
static void *
thrfunc_unlocked(void *arg)
{
LCB_CMDGET cmd = { 0 };
lcb_get3(instance, NULL, &cmd);
return NULL;
}
int main(void)
{
pthread_t thrs[10];
my_CTX ctx;
int ii;
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);
}
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;
}
Main header file for Couchbase.
void lcb_destroy(lcb_INSTANCE *instance)
Destroy (and release all allocated resources) an instance of lcb.
lcb_STATUS lcb_create(lcb_INSTANCE **instance, const lcb_CREATEOPTS *options)
Create an instance of lcb.
struct lcb_st lcb_INSTANCE
Library handle representing a connection to a cluster and its data buckets.
Definition: couchbase.h:35
lcb_STATUS lcb_connect(lcb_INSTANCE *instance)
Schedule the initial connection This function will schedule the initial connection for the handle.
lcb_STATUS lcb_wait(lcb_INSTANCE *instance, lcb_WAITFLAGS flags)
Wait for completion of scheduled operations.
@ LCB_WAIT_DEFAULT
Behave like the old lcb_wait()
Definition: couchbase.h:1854
#define LCB_CMD_SET_KEY(cmd, keybuf, keylen)
Set the key for the command.
Definition: utils.h:52