Please enlighten me on STL iterators and CriticalSection locking
-
I use STL at work, but "lazy" way, i.e. it works, and who cares about performance, right :)? So I know only basics about STL. Now I'm currently writing heavily multithreaded program for my own good. For the sake of fast development at this stage I opted to use STL. And then I noticed during debugging, that STL iterators call Critical Section locking, which causes a lot of slow down. I then delved (very briefly) into STL code and saw that they are really using CS for the iterators. I immediately dropped STL from the project, thanks to the fact that I use only arrays, not lists or maps, and there are plenty straight-forward-no-hidden-things implementations of templated arrays out there. But now I'm curious: when exactly STL iterators use CS locking? Is it for non-const iterators only? Is it for in-between-threads access? Or always? I'm too lazy and too busy to dive deep into STL code to check this, and it's not relevant to the project anymore, but out of curiosity it is very interesting, and might be useful for others who strive for performance and use STL. Thank you in advance! P.S. Forgot to mention: I use MS implementation of STL (VC++ 2005). SY- Kosta.
-
I use STL at work, but "lazy" way, i.e. it works, and who cares about performance, right :)? So I know only basics about STL. Now I'm currently writing heavily multithreaded program for my own good. For the sake of fast development at this stage I opted to use STL. And then I noticed during debugging, that STL iterators call Critical Section locking, which causes a lot of slow down. I then delved (very briefly) into STL code and saw that they are really using CS for the iterators. I immediately dropped STL from the project, thanks to the fact that I use only arrays, not lists or maps, and there are plenty straight-forward-no-hidden-things implementations of templated arrays out there. But now I'm curious: when exactly STL iterators use CS locking? Is it for non-const iterators only? Is it for in-between-threads access? Or always? I'm too lazy and too busy to dive deep into STL code to check this, and it's not relevant to the project anymore, but out of curiosity it is very interesting, and might be useful for others who strive for performance and use STL. Thank you in advance! P.S. Forgot to mention: I use MS implementation of STL (VC++ 2005). SY- Kosta.
As far as I know STL does not do any critical section locking as that is left up to the application developer. We use STL (STLPort) extensively for high-performance server-side applications and find its performance outstanding. As far as thread safety goes I would look at the Adapative Communication Framework (ACE). This library has reader/writer locks so readers don't block each other accessing a cache but a write blocks all readers. If you have a cache that is primarily read-only then this is quite and efficient mechanism.
Pax Domini sit semper vobiscum
-
As far as I know STL does not do any critical section locking as that is left up to the application developer. We use STL (STLPort) extensively for high-performance server-side applications and find its performance outstanding. As far as thread safety goes I would look at the Adapative Communication Framework (ACE). This library has reader/writer locks so readers don't block each other accessing a cache but a write blocks all readers. If you have a cache that is primarily read-only then this is quite and efficient mechanism.
Pax Domini sit semper vobiscum
I think I (almost) found answer to my own question. Here is code: #if _HAS_ITERATOR_DEBUGGING void _Orphan_range(pointer _First, pointer _Last) const { // orphan iterators within specified (inclusive) range _Lockit _Lock(_LOCK_DEBUG); const_iterator **_Pnext = (const_iterator **)&this->_Myfirstiter; while (*_Pnext != 0) if ((*_Pnext)->_Myptr < _First || _Last < (*_Pnext)->_Myptr) _Pnext = (const_iterator **)&(*_Pnext)->_Mynextiter; else { // orphan the iterator (*_Pnext)->_Mycont = 0; *_Pnext = (const_iterator *)(*_Pnext)->_Mynextiter; } } #endif /* _HAS_ITERATOR_DEBUGGING */ It's all over STL code, but it looks like locking happens only during 1) Debug Mode and 2) MultiTreaded application.