Below is an extract of the classes I have. The problem I have is with stripeLock, which is a map with pointer to clsReadWriteLock as value. The map key is stripe number, so I have one map entry for each stripe that is being locked. clsReadWriteLock contains a set, which keeps track of what jobs are locking the stripe and what type of lock. I can have multiple read jobs locking a same stripe.
Expand|Select|Wrap|Line Numbers
- class clsReadWriteLock {
- private:
- char lockType; // type of lock, r or w
- std::set<int> currentLock;
- public:
- clsReadWriteLock() {};
- clsReadWriteLock(char reqType) {lockType = reqType; currentLock.clear();}
- ~clsReadWriteLock(){currentLock.clear();}
- char getLockType() {return lockType;}
- std::set<int> getCurrentLock() {return currentLock;}
- };
- class clsLocking {
- private:
- std::map<int, clsReadWriteLock*> stripeLock;
- std::map<int, std::deque<int> > pendingWrite;
- std::map<int, std::deque<int> > pendingRead;
- public:
- clsLocking() {};
- ~clsLocking(){};
- std::map<int, clsReadWriteLock*> getStripeLock() {return stripeLock;}
- char getStripeLockType(int stripeId) {return stripeLock[stripeId]->getLockType();}
- std::set<int> getStripeLockSet(int stripeId) {return stripeLock[stripeId]->getCurrentLock();}
- .....
- }
Expand|Select|Wrap|Line Numbers
- int main() {
- clsLocking *lockObjects = new clsLocking;
- lockObjects->acquireStripeLock(1,31,'r');
- lockObjects->acquireStripeLock(1,3,'r');
- lockObjects->acquireStripeLock(1,20,'r');
- lockObjects->acquireStripeLock(2,4,'w');
- map<int, clsReadWriteLock*>::iterator mapIter;
- set<int>::iterator setIter;
- set<int> setInMap = lockObjects->getStripeLockSet(1); // uncomment this line in 2nd run
- for (mapIter= lockObjects->getStripeLock().begin(); mapIter!=lockObjects->getStripeLock().end(); mapIter++) {
- cout << "Stripe " << mapIter->first << " lock by ref Id";
- set<int> setInMap = lockObjects->getStripeLockSet(mapIter->first); // comment out this line in 2nd run
- for (setIter= setInMap.begin(); setIter!=setInMap.end(); setIter++) {
- cout << " " << (*setIter);
- }
- cout << endl << "iter->first after loop Set " << mapIter->first << endl << endl;
- }
- cout << endl;
- system("PAUSE");
- return EXIT_SUCCESS;
- }
Stripe 1 lock by ref Id 3 20 31
iter->first after loop Set 20
But if I pluck out the set outside the 1st for loop (by commenting and uncomment the 2 lines of codes as indicated above) mapIter->first remains the same and I get the following print out.
Stripe 1 lock by ref Id 3 20 31
iter->first after loop Set 1
Stripe 2 lock by ref Id 3 20 31
iter->first after loop Set 2
Why does setting the set within the loop upsets the mapIter?