RCFProto
 All Classes Functions Typedefs
win_thread.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2013, Delta V Software. All rights reserved.
6 // http://www.deltavsoft.com
7 //
8 // RCF is distributed under dual licenses - closed source or GPL.
9 // Consult your particular license for conditions of use.
10 //
11 // If you have not purchased a commercial license, you are using RCF
12 // under GPL terms.
13 //
14 // Version: 2.0
15 // Contact: support <at> deltavsoft.com
16 //
17 //******************************************************************************
18 
19 //
20 // detail/win_thread.hpp
21 // ~~~~~~~~~~~~~~~~~~~~~
22 //
23 // Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com)
24 //
25 // Distributed under the Boost Software License, Version 1.0. (See accompanying
26 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
27 //
28 
29 #ifndef RCF_DETAIL_WIN_THREAD_HPP
30 #define RCF_DETAIL_WIN_THREAD_HPP
31 
32 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
33 # pragma once
34 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
35 
36 #if defined(BOOST_WINDOWS) && !defined(UNDER_CE)
37 
38 #include <RCF/thread/push_options.hpp>
39 
40 namespace RCF {
41 namespace detail {
42 
43 RCF_EXPORT unsigned int __stdcall win_thread_function(void* arg);
44 
45 class RCF_EXPORT win_thread
46  : private noncopyable/*,
47  public win_thread_base<win_thread>*/
48 {
49 public:
50  // Constructor.
51  template <typename Function>
52  win_thread(Function f, unsigned int stack_size = 0)
53  : thread_(0),
54  exit_event_(0)
55  {
56  start_thread(new func<Function>(f), stack_size);
57  }
58 
59  // Destructor.
60  ~win_thread();
61 
62  // Wait for the thread to exit.
63  void join();
64 
65 private:
66  friend RCF_EXPORT unsigned int __stdcall win_thread_function(void* arg);
67 
68  class func_base
69  {
70  public:
71  virtual ~func_base() {}
72  virtual void run() = 0;
73  ::HANDLE entry_event_;
74  ::HANDLE exit_event_;
75  };
76 
77  struct auto_func_base_ptr
78  {
79  func_base* ptr;
80  ~auto_func_base_ptr() { delete ptr; }
81  };
82 
83  template <typename Function>
84  class func
85  : public func_base
86  {
87  public:
88  func(Function f)
89  : f_(f)
90  {
91  }
92 
93  virtual void run()
94  {
95  f_();
96  }
97 
98  private:
99  Function f_;
100  };
101 
102  void start_thread(func_base* arg, unsigned int stack_size);
103 
104  ::HANDLE thread_;
105  ::HANDLE exit_event_;
106 };
107 
108 } // namespace detail
109 } // namespace RCF
110 
111 #include <RCF/thread/pop_options.hpp>
112 
113 #endif // defined(BOOST_WINDOWS) && !defined(UNDER_CE)
114 
115 #endif // RCF_DETAIL_WIN_THREAD_HPP