RCFProto

- Main Page
- Classes
- Files

- File List

All Classes Functions Typedefs
posix_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/posix_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_POSIX_THREAD_HPP
30 #define RCF_DETAIL_POSIX_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_HAS_PTHREADS)
37
38 #include <pthread.h>
39
40 #include <RCF/thread/push_options.hpp>
41
42 namespace RCF {
43 namespace detail {
44
45 extern "C"
46 {
47 RCF_EXPORT void* RCF_detail_posix_thread_function(void* arg);
48 }
49
50 class posix_thread
51 : private noncopyable
52 {
53 public:
54 // Constructor.
55 template <typename Function>
56 posix_thread(Function f)
57 : joined_(false)
58 {
59 start_thread(new func<Function>(f));
60 }
61
62 // Destructor.
63 RCF_EXPORT ~posix_thread();
64
65 // Wait for the thread to exit.
66 RCF_EXPORT void join();
67
68 private:
69 friend void* RCF_detail_posix_thread_function(void* arg);
70
71 class func_base
72 {
73 public:
74 virtual ~func_base() {}
75 virtual void run() = 0;
76 };
77
78 struct auto_func_base_ptr
79 {
80 func_base* ptr;
81 ~auto_func_base_ptr() { delete ptr; }
82 };
83
84 template <typename Function>
85 class func
86 : public func_base
87 {
88 public:
89 func(Function f)
90 : f_(f)
91 {
92 }
93
94 virtual void run()
95 {
96 f_();
97 }
98
99 private:
100 Function f_;
101 };
102
103 RCF_EXPORT void start_thread(func_base* arg);
104
105 ::pthread_t thread_;
106 bool joined_;
107 };
108
109 } // namespace detail
110 } // namespace RCF
111
112 #include <RCF/thread/pop_options.hpp>
113
114 #endif // defined(BOOST_HAS_PTHREADS)
115
116 #endif // RCF_DETAIL_POSIX_THREAD_HPP