By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,384 Members | 671 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,384 IT Pros & Developers. It's quick & easy.

STL: Set within Map upsets iterator

P: 1
I'm trying to implement some kind of locking and am having problem with iterating through a map that contains an object with a set. I can of course change my structure and to aviod the problem, but I'm REALLY curious why this is happening.

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
  1. class clsReadWriteLock {
  2.    private:
  3.      char           lockType;   // type of lock, r or w
  4.      std::set<int>  currentLock;
  5.    public:
  6.      clsReadWriteLock() {};
  7.      clsReadWriteLock(char reqType) {lockType = reqType; currentLock.clear();}
  8.      ~clsReadWriteLock(){currentLock.clear();}
  9.  
  10.      char         getLockType() {return lockType;}
  11.      std::set<int>  getCurrentLock() {return currentLock;}
  12. };
  13.  
  14. class clsLocking {
  15.    private:
  16.       std::map<int, clsReadWriteLock*>  stripeLock;
  17.       std::map<int, std::deque<int> >   pendingWrite;
  18.       std::map<int, std::deque<int> >      pendingRead;
  19.  
  20.     public:
  21.       clsLocking() {};
  22.       ~clsLocking(){};
  23.  
  24.       std::map<int, clsReadWriteLock*> getStripeLock() {return stripeLock;}
  25.       char     getStripeLockType(int stripeId) {return stripeLock[stripeId]->getLockType();}
  26.       std::set<int>     getStripeLockSet(int stripeId) {return stripeLock[stripeId]->getCurrentLock();}
  27.     .....
  28. }
Below is my main(), where I put values into the map. Stripe 1 is lock by jobs 31, 2 and 20, and stripe 2 is lock by job 4. However, when I try to iterate the map to print out the jobs tied to each stripe, the iterator for the map keeps giving me problem. After the 2nd for loop, value of mapIter->first gets changed if I try to pluck out the set that is in the map.

Expand|Select|Wrap|Line Numbers
  1. int main() {
  2.  
  3.    clsLocking *lockObjects = new clsLocking;
  4.    lockObjects->acquireStripeLock(1,31,'r');
  5.    lockObjects->acquireStripeLock(1,3,'r');
  6.    lockObjects->acquireStripeLock(1,20,'r');
  7.    lockObjects->acquireStripeLock(2,4,'w');
  8.  
  9.    map<int, clsReadWriteLock*>::iterator mapIter;
  10.    set<int>::iterator setIter;
  11.    set<int> setInMap = lockObjects->getStripeLockSet(1);        // uncomment this line in 2nd run
  12.    for (mapIter= lockObjects->getStripeLock().begin(); mapIter!=lockObjects->getStripeLock().end(); mapIter++) {
  13.        cout << "Stripe " << mapIter->first << " lock by ref Id";
  14.        set<int> setInMap = lockObjects->getStripeLockSet(mapIter->first);    // comment out this line in 2nd run
  15.        for (setIter= setInMap.begin(); setIter!=setInMap.end(); setIter++) {
  16.           cout << "  " << (*setIter);
  17.        }
  18.        cout << endl << "iter->first after loop Set " << mapIter->first << endl << endl;
  19.   }
  20.     cout << endl;
  21.     system("PAUSE");
  22.     return EXIT_SUCCESS;
  23. }
I get the printout below and the program terminates.
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?
Apr 20 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.