方法
pthread_mutex_lock
中对get
的调用永远阻塞(在使用地图的场景中,构造函数和对put
的一些调用正确执行)。可能是什么原因?
template <typename K, typename V>
class SimpleConcurrentHashMap {
private:
pthread_mutex_t _lock;
int _numberBuckets;
V _defaultValue;
SimpleConcurrentHashMapEntry<K, V>** _buckets;
int(*_mapKeyToInteger)(K);
int hash(const K& key) {
return _mapKeyToInteger(key) % _numberBuckets;
}
public:
SimpleConcurrentHashMap(int numberBuckets, int(*mapKeyToInteger)(K), const V& defaultValue) {
_lock = PTHREAD_MUTEX_INITIALIZER;
if(pthread_mutex_init(&_lock, NULL) != 0) {
exit(1);
}
pthread_mutex_lock(&_lock);
_numberBuckets = numberBuckets;
_mapKeyToInteger = mapKeyToInteger;
_defaultValue = defaultValue;
_buckets = (SimpleConcurrentHashMapEntry<K, V>**)malloc(_numberBuckets * sizeof(SimpleConcurrentHashMapEntry<K, V>*));
for(int i = 0; i < _numberBuckets; i += 1) {
_buckets[i] = nullptr;
}
pthread_mutex_unlock(&_lock);
}
~SimpleConcurrentHashMap() {
pthread_mutex_lock(&_lock);
for(int i = 0; i < _numberBuckets; i += 1) {
SimpleConcurrentHashMapEntry<K, V>* current = _buckets[i];
while (current != nullptr) {
SimpleConcurrentHashMapEntry<K, V>* next = current->next;
delete current;
current = next;
}
_buckets[i] = nullptr;
}
delete _buckets;
pthread_mutex_unlock(&_lock);
pthread_mutex_destroy(&_lock);
}
void put(const K& key, const V& value) {
pthread_mutex_lock(&_lock);
int bucketIndex = hash(key);
SimpleConcurrentHashMapEntry<K, V>* current = _buckets[bucketIndex];
while(current != nullptr) {
if(current->key == key) {
current->value = value;
pthread_mutex_unlock(&_lock);
return;
}
current = current->next;
}
SimpleConcurrentHashMapEntry<K, V>* newEntry = new SimpleConcurrentHashMapEntry<K, V>();
newEntry->key = key;
newEntry->value = value;
newEntry->next = _buckets[bucketIndex];
_buckets[bucketIndex] = newEntry;
pthread_mutex_unlock(&_lock);
}
const V& get(const K& key) {
pthread_mutex_lock(&_lock);
int bucketIndex = hash(key);
SimpleConcurrentHashMapEntry<K, V>* current = _buckets[bucketIndex];
while(current != nullptr) {
if(current->key == key) {
const V& result = current->value;
pthread_mutex_unlock(&_lock);
return result;
}
current = current->next;
}
pthread_mutex_unlock(&_lock);
return _defaultValue;
}
};