CARMA C++
AutoPthreadQuitAndJoinGroup.h
1 #ifndef CARMA_UTIL_AUTO_PTHREAD_QUIT_AND_JOIN_GROUP_H
2 #define CARMA_UTIL_AUTO_PTHREAD_QUIT_AND_JOIN_GROUP_H
3 
4 #include <map>
5 
6 #include <pthread.h>
7 
8 namespace carma {
9 namespace util {
10 
11 
12 class AutoPthreadQuitAndJoinGroup {
13  public:
14  typedef enum {
15  QUIT_AND_JOIN_ACTION,
16  QUIT_ONLY_ACTION,
17  JOIN_ONLY_ACTION
18  } Action;
19 
20  explicit AutoPthreadQuitAndJoinGroup();
21 
22  explicit AutoPthreadQuitAndJoinGroup( ::pthread_t thread );
23 
24  explicit AutoPthreadQuitAndJoinGroup( ::pthread_t thread,
25  Action action );
26 
27  virtual ~AutoPthreadQuitAndJoinGroup( );
28 
29  void swap( AutoPthreadQuitAndJoinGroup & rhs );
30 
31  void insert( ::pthread_t thread );
32 
33  void insert( ::pthread_t thread,
34  Action action );
35 
36  void remove( ::pthread_t thread );
37 
38  void requestQuitsNoThrow( ) const;
39 
40  private:
41  // No copying
42  AutoPthreadQuitAndJoinGroup( const AutoPthreadQuitAndJoinGroup & rhs );
43  AutoPthreadQuitAndJoinGroup &
44  operator=( const AutoPthreadQuitAndJoinGroup & rhs );
45 
46  typedef ::std::map< ::pthread_t, Action > ThreadsMap;
47 
48  ThreadsMap threads_;
49 };
50 
51 
52 } // namespace carma::util
53 } // namespace carma
54 
55 
56 // ******* Below here is simply implementation *******
57 
58 inline
59 carma::util::AutoPthreadQuitAndJoinGroup::AutoPthreadQuitAndJoinGroup( ) :
60 threads_()
61 {
62 }
63 
64 
65 inline
66 carma::util::AutoPthreadQuitAndJoinGroup::AutoPthreadQuitAndJoinGroup(
67  const ::pthread_t thread ) :
68 threads_()
69 {
70  threads_.insert( ::std::make_pair( thread, QUIT_AND_JOIN_ACTION ) );
71 }
72 
73 
74 inline
75 carma::util::AutoPthreadQuitAndJoinGroup::AutoPthreadQuitAndJoinGroup(
76  const ::pthread_t thread,
77  const Action action ) :
78 threads_()
79 {
80  threads_.insert( ::std::make_pair( thread, action ) );
81 }
82 
83 
84 #endif