Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TQCantAlloc : ResourceException
- {
- };
- template<typename T>
- class ThreadedQueue
- {
- public:
- ThreadedQueue( bool fThrowOnFailue = true );
- void Push( T &t );
- T Pop();
- private:
- CondVar m_cv;
- std::list<T> m_lstT;
- bool m_fThrowOnFailue;
- int m_failures;
- };
- template<typename T>
- ThreadedQueue<T>::ThreadedQueue( bool fThrowOnFailue )
- {
- m_fThrowOnFailue = fThrowOnFailue;
- m_failures = 0;
- }
- template<typename T>
- void ThreadedQueue<T>::Push( T &t )
- {
- CondVar::Guard cvg( &m_cv );
- try
- {
- m_lstT.push_back( t );
- }
- catch( std::exception & )
- {
- if( m_fThrowOnFailue )
- m_failures++;
- cvg.Release();
- throw TQCantAlloc();
- }
- cvg.Release();
- }
- template<typename T>
- T ThreadedQueue<T>::Pop()
- {
- CondVar::Guard cvg( &m_cv );
- T t;
- while( m_lstT.empty() && m_failures == 0 )
- cvg.Wait();
- if( m_failures )
- m_failures--,
- throw TQCantAlloc();
- t = m_lstT.front();
- m_lstT.pop_front();
- return t;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement