1、put()操作没有加锁。
* 1个线程时每次都会找到不同的unused entries并写入,不会miss
* 当2个线程都运行到put() line61并找到同一个unused entry时,先写入的key就Miss了。
***
2、
- 修改:为put()函数加锁。第59行进入for循环前lock(), 两处return前unlock()
- 原因:这样保证同时只有1个线程可以执行put()操作
```cpp
assert(pthread_mutex_lock(&lock) == 0);
for (i = 0; i < NENTRY; i++) {
if (!table[b][i].inuse) {
table[b][i].key = key;
table[b][i].value = value;
table[b][i].inuse = 1;
assert(pthread_mutex_unlock(&lock) == 0);
return;
}
}
assert(pthread_mutex_unlock(&lock) == 0);
```
***
3、put的时长:
- 单线程 = 1.667020
- 双线程 = 2.083564
***
4、因为每次lock和unlock消耗时间,会有一个线程等待另一个线程释放锁。
***
5、因为双线程可以同时进行get()操作且不用在拿锁放锁上消耗时间。
***
6、
***
7、
***
Question 6: Why does valgrind report no errors for get()? Can you imagine a execution sequence where valgrind may also report error for get(), with current get() and put() functions?
Challenge
Question 7: Can you think of a way of modifying ph.c so that you get speedup for both phases and valgrind won't report race conditions? (If you have time, implement that plan and check.)