Seg3D  2.4
Seg3D is a free volume segmentation and processing tool developed by the NIH Center for Integrative Biomedical Computing at the University of Utah Scientific Computing and Imaging (SCI) Institute.
the_boost_thread.hxx
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2016 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 
30 // File : the_boost_thread.hxx
31 // Author : Pavel A. Koshevoy
32 // Created : Sat Oct 25 12:35:09 MDT 2008
33 // Copyright : (C) 2004-2008 University of Utah
34 // Description : A thin wrapper for Boost thread class.
35 
36 #ifndef THE_BOOST_THREAD_HXX_
37 #define THE_BOOST_THREAD_HXX_
38 
39 // Boost includes:
40 #include <boost/thread/thread.hpp>
41 #include <boost/thread/mutex.hpp>
42 #include <boost/thread/tss.hpp>
43 
44 // local includes:
45 #include <Core/ITKCommon/ThreadUtils/the_terminator.hxx>
46 #include <Core/ITKCommon/ThreadUtils/the_thread_interface.hxx>
47 
48 // forward declarations:
51 
52 
53 //----------------------------------------------------------------
54 // the_boost_terminators_t
55 //
57 {
58 public:
59  // virtual: concurrent access controls:
60  void lock() { mutex_.lock(); }
61  void unlock() { mutex_.unlock(); }
62 
63 private:
64  mutable boost::mutex mutex_;
65 };
66 
67 
68 //----------------------------------------------------------------
69 // the_boost_thread_t
70 //
71 // 1. the thread will not take ownership of the transactions.
72 // 2. the thread will take ownership of the mutex.
73 //
75 {
76 private:
77  struct callable_t
78  {
79  callable_t(the_boost_thread_t * thread):
80  thread_(thread)
81  {}
82 
83  void operator()()
84  {
85  thread_->run();
86  }
87 
88  private:
89  the_boost_thread_t * thread_;
90  };
91 
92 public:
94 
95  // the destructor is protected on purpose,
96  // see delete_this for details:
97  virtual ~the_boost_thread_t();
98 
99  // In order to avoid memory management problems with shared libraries,
100  // whoever provides this interface instance (via it's creator), has to
101  // provide a way to delete the instance as well. This will avoid
102  // issues with multiple-instances of C runtime libraries being
103  // used by the app and whatever libraries it links against that
104  // either use or provide this interface:
105  virtual void delete_this();
106 
107  // the creation method:
108  static the_thread_interface_t * create()
109  { return new the_boost_thread_t(); }
110 
111  // the thread storage accessor:
112  static the_thread_storage_t & thread_storage();
113 
114  // virtual: start the thread:
115  void start();
116 
117  // virtual:
118  void wait();
119 
120  // virtual: put the thread to sleep:
121  void take_a_nap(const unsigned long & microseconds);
122 
123  // virtual: accessor to the transaction terminators:
124  the_terminators_t & terminators();
125 
126 protected:
127  // virtual:
128  void run();
129 
130  // the boost thread:
131  boost::thread * boost_thread_;
132 
133  // a list of active terminators for this thread:
134  the_boost_terminators_t terminators_;
135 };
136 
137 
138 #endif // THE_BOOST_THREAD_HXX_
Definition: the_boost_thread.hxx:74
Definition: the_boost_thread.hxx:56
Definition: the_mutex_interface.hxx:42
Definition: the_thread_storage.hxx:58
Definition: the_terminator.hxx:120
Definition: the_thread_interface.hxx:57