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 {
pthread_mutex_t mutex;
} my_CTX;
static void *
thrfunc_locked(void *arg)
{
my_CTX *ctx = arg;
pthread_mutex_lock(&ctx->mutex);
pthread_mutex_unlock(&ctx->mutex);
return NULL;
}
static void *
thrfunc_unlocked(void *arg)
{
lcb_get_cmd_t cmd = { 0 }, *cmdp = &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;
}
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.