-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththread_safe_stack.h
More file actions
31 lines (21 loc) · 1 KB
/
thread_safe_stack.h
File metadata and controls
31 lines (21 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#ifndef THREAD_SAFE_STACK_H_INCLUDED
#define THREAD_SAFE_STACK_H_INCLUDED
#include <deque>
#include <boost/thread.hpp>
namespace thread_safe {
template < class T, class Container = std::deque<T> >
class stack {
public:
explicit stack( const Container & ctnr = Container() ) : storage( ctnr ) { }
bool empty( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.empty(); }
size_t size( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.size(); }
T & top( void ) { boost::lock_guard<boost::mutex> lock( mutex ); return storage.back(); }
const T & top( void ) const { boost::lock_guard<boost::mutex> lock( mutex ); return storage.back(); }
void push( const T & u ) { boost::lock_guard<boost::mutex> lock( mutex ); storage.push_back( u ); }
void pop( void ) { boost::lock_guard<boost::mutex> lock( mutex ); storage.pop_back(); }
private:
Container storage;
mutable boost::mutex mutex;
};
}
#endif // THREAD_SAFE_STACK_H_INCLUDED