diff options
author | Mole Shang <[email protected]> | 2024-02-16 11:29:36 +0800 |
---|---|---|
committer | Mole Shang <[email protected]> | 2024-02-16 11:31:23 +0800 |
commit | edd523ffcb39c1c57944796fabfc71c70a10ce2e (patch) | |
tree | a37eaf56bbee9509f32a775793a61738b3d4bbac /notxv6/ph.c | |
parent | a98c56a811142e5ede3332a7a444cca45f628769 (diff) | |
download | xv6-labs-edd523ffcb39c1c57944796fabfc71c70a10ce2e.tar.gz xv6-labs-edd523ffcb39c1c57944796fabfc71c70a10ce2e.tar.bz2 xv6-labs-edd523ffcb39c1c57944796fabfc71c70a10ce2e.zip |
lab thread: finish
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(); |