Shows how to access a database with multiple threads.
#ifndef _WIN32
#include <pthread.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include "windows_shim.h"
#endif
#include <wiredtiger.h>
static const char *home;
void *scan_thread(void *arg);
#define NUM_THREADS 10
void *
scan_thread(void *conn_arg)
{
const char *key, *value;
int ret;
conn = conn_arg;
session, "table:access", NULL, NULL, &cursor);
while ((ret = cursor->
next(cursor)) == 0) {
ret = cursor->
get_key(cursor, &key);
printf("Got record: %s : %s\n", key, value);
}
fprintf(stderr,
"WT_CURSOR.next: %s\n", session->
strerror(session, ret));
return (NULL);
}
int
main(void)
{
pthread_t threads[NUM_THREADS];
int i, ret;
if (getenv("WIREDTIGER_HOME") == NULL) {
home = "WT_HOME";
ret = system("rm -rf WT_HOME && mkdir WT_HOME");
} else
home = NULL;
fprintf(stderr, "Error connecting to %s: %s\n",
ret = session->
create(session,
"table:access",
"key_format=S,value_format=S");
ret = session->
open_cursor(session,
"table:access", NULL,
"overwrite", &cursor);
ret = session->
close(session, NULL);
for (i = 0; i < NUM_THREADS; i++)
ret = pthread_create(&threads[i], NULL, scan_thread, conn);
for (i = 0; i < NUM_THREADS; i++)
ret = pthread_join(threads[i], NULL);
ret = conn->
close(conn, NULL);
return (ret);
}