473,499 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL: Set within Map upsets iterator

1 New Member
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
0 3013

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

Similar topics

7
1386
by: Patrick | last post by:
In class *ClassA* below I have an STL Vector *vec* as a member variable of the class. Do I have to create a destructer, and somehow deallocate the memory from *vec*, or is this handled...
4
3523
by: Merlin | last post by:
Hi, I am a C++ developer and would like to implement container classes for various types of objects. I use MS Visual C++ to compile my code. Nevertheless I like to write code that is independent...
11
2548
by: RR | last post by:
I have Plauger's book on STL and I've found that it's different from the STL I have on a Linux box (libstdc++-2.96-112). In particular, the map::erase(iterator) method I have returns nothing...
5
2550
by: Draw | last post by:
Hi All, Just a thought, about the find() algorithm in the C++ STL. I read that the find algorithm can take a range of iterators. If it does not find the element it is looking for in that range...
8
6154
by: olanglois | last post by:
Hi, I was asking myself to following question. What is better to erase an element from a STL map: calling (option #1) size_type erase(const key_type& k) or calling (option #2)
9
4591
by: Christian Chrismann | last post by:
Hi, I've a runtime problem with STL vectors. Here is the simplified version of the code: template <class Tclass A { ... private: vector<T*myvector; typename vector<T*>::itarator mIt;
9
2208
by: Jeff Dege | last post by:
I've been programming in C++ for a good long while, but there are aspects of the language I've never needed, and hence never bothered to really learn. It's the curse of working on a developed...
5
1782
by: cai_xiaodong | last post by:
I am very new to STL and generic programming. PLEASE help. I have my own class looks like: class pixelProperty { public: // get-functions here... private: Point mPosition; int ...
2
3045
by: Rakesh Kumar | last post by:
I am encountering the following issue with STL map iterator - wrapped within a template. ( I am wrapping it withing a template since I want to hide the map implementation from the user . At a later...
0
7130
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7386
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3090
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.