Skip to content

Commit

Permalink
lab thead task 2
Browse files Browse the repository at this point in the history
  • Loading branch information
relaxcn committed Apr 6, 2024
1 parent a965c2b commit b95669c
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion notxv6/ph.c
Expand Up @@ -17,6 +17,8 @@ struct entry *table[NBUCKET];
int keys[NKEYS];
int nthread = 1;

pthread_mutex_t lock[NBUCKET]; // lock for puts


double
now()
Expand All @@ -31,7 +33,7 @@ insert(int key, int value, struct entry **p, struct entry *n)
{
struct entry *e = malloc(sizeof(struct entry));
e->key = key;
e->value = value;
e->value = value;
e->next = n;
*p = e;
}
Expand All @@ -51,8 +53,11 @@ void put(int key, int value)
// update the existing key.
e->value = value;
} else {
pthread_mutex_lock(&lock[i]);
// the new is new.
// 重要的是 table[i] 的值,如果 thread1 刚进入,但是 thread_2 已经修改了 table[i] 此时就会丢失后面的所有node
insert(key, value, &table[i], table[i]);
pthread_mutex_unlock(&lock[i]);
}

}
Expand Down Expand Up @@ -118,6 +123,9 @@ main(int argc, char *argv[])
keys[i] = random();
}

for (int i = 0; i < NBUCKET; ++i)
pthread_mutex_init(&lock[i], NULL);

//
// first the puts
//
Expand Down

0 comments on commit b95669c

Please sign in to comment.