how i an erratic twine close c++?


let's contend i have multithreaded c++ way handles requests form duty handlerequest(string key). any handlerequest occurs removed thread, there an arbitrarily infinite array illusory values key.



i wish following behavior:




  • simultaneous calls handlerequest(key) serialized have same value key.

  • global serialization minimized.



the physique handlerequest competence feeling this:



void handlerequest(string key) {
keylock lock(key);
// hoop request.
}


question: i exercise keylock mandatory behavior?



a genuine doing competence start off this:



keylock::keylock(string key) {
global_lock->lock();
internal_lock_ = global_key_map[key];
(internal_lock_ == null) {
internal_lock_ = new lock();
global_key_map[key] = internal_lock_;
}
global_lock->unlock();
internal_lock_->lock();
}

keylock::~keylock() {
internal_lock_->unlock();
// mislay internal_lock_ global_key_map iff threads sharp it.
}


...but requires tellurian close during commencement finish any request, origination removed lock vigilant any request. quarrel high between calls handlerequest, competence problem, nonetheless levy lot over quarrel low.



Comments