1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| #include <assert.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <time.h> #include <hiredis/hiredis.h> #include <pthread.h>
#define SERV_IP "127.0.0.1" #define SERV_PORT 6379 #define NUM_READER 100 #define NUM_WRITER 100
static const int OK = 0; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int DBGetString(const char *key, char *value) { struct timeval tv; tv.tv_sec = 5; tv.tv_usec = 0; redisContext *c = redisConnectWithTimeout(SERV_IP, SERV_PORT, tv); assert(c != NULL && !c->err);
char cmd[256]; sprintf(cmd, "GET %s", key); redisReply *reply = (redisReply *)redisCommand(c, cmd); assert(reply->str != NULL); strcpy(value, reply->str); freeReplyObject(reply); redisFree(c); return OK; }
int DBSetString(const char *key, const char *value) { struct timeval tv; tv.tv_sec = 5; tv.tv_usec = 0; redisContext *c = redisConnectWithTimeout(SERV_IP, SERV_PORT, tv); assert(c != NULL && !c->err);
char cmd[256]; sprintf(cmd, "SET %s %s", key, value); redisReply *reply = (redisReply *)redisCommand(c, cmd); freeReplyObject(reply); redisFree(c); return OK; }
void *Reader(void *args) { char buf[256]; DBGetString("hello", buf); printf("%s\n", buf); return NULL; }
void *Writer(void *args) { DBSetString("hello", "world"); return NULL; }
void testcase() { time_t t1 = time(NULL); pthread_t wid[NUM_WRITER]; pthread_t rid[NUM_READER];
for (int i = 0; i < NUM_WRITER; ++i) { pthread_create(&wid[i], NULL, Writer, NULL); } for (int i = 0; i < NUM_READER; ++i) { pthread_create(&rid[i], NULL, Reader, NULL); }
for (int i = 0; i < NUM_WRITER; ++i) { pthread_join(wid[i], NULL); } for (int i = 0; i < NUM_READER; ++i) { pthread_join(rid[i], NULL); } time_t t2 = time(NULL); printf("%ld s\n", t2 - t1); }
int main() { testcase(); return 0; }
|