S********t 发帖数: 3431 | 1 大家都知道lock吧,thread synchronization用的。
这个题目是这样的:
假设我们有一个现成的library提供了基本的reader/writer lock功能:
class Lock {
public:
Lock();
// exclusive writer lock
void GetLock();
void ReleaseLock();
// shared reader lock
void GetReaderLock();
void ReleaseReaderLock();
};
Problem 1: implement a functionality to atomically upgrade a reader lock to
writer lock, if no one else is holding reader lock
Problem 2: implement a functionality to manage lock per keys, i.e.:
template
class LockManager {
void GetLock(KeyType key);
void ReleaseLock(KeyType key);
void GetReaderLock(KeyType key);
void ReleaseReaderLock(KeyType key);
}
requirement:
a. able to lock/unlock on different keys concurrently
b. keep memory space linear to the size of "active keys", i.e., if no one is
holding lock for a key, you need to do garbage collect for that key. | c********t 发帖数: 5706 | |
|