Skip to content

Instantly share code, notes, and snippets.

@hirokuma
Created February 24, 2019 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hirokuma/98f8d064f3bd4e049b3026da9190ae76 to your computer and use it in GitHub Desktop.
Save hirokuma/98f8d064f3bd4e049b3026da9190ae76 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "lmdb.h"
#define DBDIR "./testdb"
static MDB_env *env;
static void *thread_func(void *pArg)
{
int code = (int)pArg; //warningが出るが、気にしない
int rc;
MDB_txn *txn;
unsigned long flags = 0;
switch (code) {
case 1:
flags = 0;
break;
case 2:
sleep(1);
flags = MDB_RDONLY;
break;
}
fprintf(stderr, "txn begin: code=%d\n", code);
rc = mdb_txn_begin(env, NULL, flags, &txn);
if (rc) {
fprintf(stderr, "fail: mdb_txn_begin(%d:%s)\n", code, mdb_strerror(rc));
goto LABEL_EXIT2;
}
printf("sleeping(%d)...\n", code);
sleep(10);
printf("wake up(%d)\n", code);
mdb_txn_abort(txn);
LABEL_EXIT2:
return NULL;
}
int main(void)
{
int rc;
mkdir(DBDIR, 0755);
rc = mdb_env_create(&env);
if (rc) {
fprintf(stderr, "fail: mdb_env_create(%s)\n", mdb_strerror(rc));
}
rc = mdb_env_open(env, DBDIR, MDB_FIXEDMAP, 0664);
if (rc) {
fprintf(stderr, "fail: mdb_env_open(%s)\n", mdb_strerror(rc));
goto LABEL_EXIT1;
}
pthread_t th1;
pthread_create(&th1, NULL, &thread_func, (void *)1);
pthread_t th2;
pthread_create(&th2, NULL, &thread_func, (void *)2);
pthread_join(th1, NULL);
pthread_join(th2, NULL);
LABEL_EXIT1:
mdb_env_close(env);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment