diff options
Diffstat (limited to 'notxv6/ph.c')
-rw-r--r-- | notxv6/ph.c | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/notxv6/ph.c b/notxv6/ph.c index 82afe76..db8a630 100644 --- a/notxv6/ph.c +++ b/notxv6/ph.c @@ -16,7 +16,7 @@ struct entry { struct entry *table[NBUCKET]; int keys[NKEYS]; int nthread = 1; - +pthread_mutex_t lock[NBUCKET]; double now() @@ -47,6 +47,7 @@ void put(int key, int value) if (e->key == key) break; } + pthread_mutex_lock(&lock[i]); if(e){ // update the existing key. e->value = value; @@ -54,7 +55,7 @@ void put(int key, int value) // the new is new. insert(key, value, &table[i], table[i]); } - + pthread_mutex_unlock(&lock[i]); } static struct entry* @@ -67,7 +68,6 @@ get(int key) for (e = table[i]; e != 0; e = e->next) { if (e->key == key) break; } - return e; } @@ -113,6 +113,9 @@ main(int argc, char *argv[]) nthread = atoi(argv[1]); tha = malloc(sizeof(pthread_t) * nthread); srandom(0); + for (int i = 0; i < NBUCKET; i++) { + pthread_mutex_init(&lock[i], NULL); + } assert(NKEYS % nthread == 0); for (int i = 0; i < NKEYS; i++) { keys[i] = random(); |