Skip to content

Instantly share code, notes, and snippets.

@zed-0xff
Last active August 29, 2015 14:22
Show Gist options
  • Save zed-0xff/8f1db57c4d5127f9f282 to your computer and use it in GitHub Desktop.
Save zed-0xff/8f1db57c4d5127f9f282 to your computer and use it in GitHub Desktop.
#include <string.h>
#include <sys/fcntl.h>
#include <mdbm/mdbm.h>
#include <mdbm/mdbm_handle_pool.h>
#include <stdlib.h>
#include <pthread.h>
#include <thread>
#include <iostream>
#include <vector>
struct mdbm_key_t {
uint32_t k1;
};
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int nthreads = 2;
int niterations = 100000;
void threadfunc(mdbm_pool_t* pool, int idx) {
std::thread::id tid = std::this_thread::get_id();
MDBM* db = mdbm_pool_acquire_handle(pool);
if( db == NULL ){
perror("mdbm_pool_acquire_handle");
exit( EXIT_FAILURE );
}
srand(time(NULL));
// main processing here
mdbm_key_t key;
uint32_t data1, data2;
datum d_key, d_val;
d_key.dptr = (char*)&key;
d_key.dsize = sizeof(key);
d_val.dptr = (char*)&key;
d_val.dsize = sizeof(key);
for(int i=0; i<niterations; i++) {
key.k1 = rand(); //%10000;
data2++;
//std::cout << "thread " << tid << " locking key " << key.k1 << " .." << std::endl;
if( mdbm_plock(db, &d_key, 0) != 1 ){
perror("mdbm_plock");
exit( EXIT_FAILURE );
}
//std::cout << "thread " << tid << " storing key " << key.k1 << " .." << std::endl;
if( mdbm_store(db,d_key,d_val,MDBM_REPLACE) == -1 ) {
perror("mdbm_store");
exit(1);
}
//std::cout << "thread " << tid << " unlocking key " << key.k1 << " .." << std::endl;
if( mdbm_punlock(db, &d_key, 0) != 1 ) {
perror("mdbm_punlock");
exit( EXIT_FAILURE );
}
}
// end main processing
mdbm_pool_release_handle( pool, db );
std::cout << "thread " << tid << " done" << std::endl;
}
int main(int argc, char **argv)
{
char fn[2048];
char buf[2048];
int ndups;
std::vector <std::thread> threads;
std::vector <MDBM*> pdbms;
if( argc == 3 ){
nthreads = atoi(argv[1]);
niterations = atoi(argv[2]);
}
std::cout << "nthreads = " << nthreads << ", niterations = " << niterations << std::endl;
snprintf(fn,sizeof(fn),"%s","example.mdbm");
MDBM* db = mdbm_open(fn,MDBM_O_RDWR|MDBM_O_CREAT|MDBM_SINGLE_ARCH|MDBM_PARTITIONED_LOCKS,0666,0,0);
if (!db) {
perror("Unable to create database");
exit( EXIT_FAILURE );
}
mdbm_pool_t *pool = mdbm_pool_create_pool(db, nthreads);
if (pool == NULL) {
fprintf(stderr, "pool is null, aborting!");
exit(1);
}
for( int i=0; i<nthreads; i++ ){
threads.emplace_back( threadfunc, pool, i );
//threads.emplace_back( threadfunc, db );
}
for( auto& thread : threads )
thread.join();
std::cout << "all threads done" << std::endl;
mdbm_pool_destroy_pool(pool);
mdbm_close(db);
return 0;
}
@areese
Copy link

areese commented Jun 4, 2015

I'm pretty sure it remains locked if you hit this line:
https://gist.github.com/zed-0xff/8f1db57c4d5127f9f282#file-gistfile1-cpp-L56

Also for our use cases we typically do this:

pool.get();
mdbm_lock()
mdbm_*
mdbm_unlock()
pool.return();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment