5 #ifndef __SFJP_ROAST__parallel__thread__thread_HPP__
6 #define __SFJP_ROAST__parallel__thread__thread_HPP__
8 #include "roast/_common.hpp"
12 ///////////////////////////////////////////////////////
14 ROAST_DECL_EXCEPTION(thread_exception);
16 ////////////////////////////////////////////////////////////
18 template <typename _Impl>
22 typedef void* (*callback_func_t)(void*);
23 typedef typename _Impl::handle_type handle_t;
30 void handle_assert(const char* method_name){
31 if ( m_handle == _Impl::invalid_handle )
32 throw thread_exception(::std::string(method_name) + "(): Handler is Invalid.");
37 m_handle = _Impl::invalid_handle;
40 /*thread_(callback_func_t func, int param=0, void* opt=NULL)
42 if ( start(func,param,opt) != true )
43 thread_exception("thread start failed.");
45 thread_(thread_& th){} // TODO
47 template <typename _Func>
53 template <typename _Func, typename _Param>
54 thread_(_Func func, _Param param, void* opt=NULL)
56 m_handle = _Impl::invalid_handle;
57 if ( start(func,param,opt) != true )
58 thread_exception("thread start failed.");
61 //#ifndef _ROAST_INVALID_TEMPLATE_OVERLOAD
62 template <typename _Class, typename _Param, typename _Ret>
63 thread_(_Class* p_this, _Ret (_Class::*func)(_Param), _Param param=0, void* opt=NULL)
65 m_handle = _Impl::invalid_handle;
66 if ( start_class_ptr(p_this,func,param,opt) != true )
67 thread_exception("thread start failed.");
75 ////////////////////////////////////////////////////////
77 bool start(callback_func_t func, void* param=NULL, void* opt=NULL)
79 return start_func(func,param,opt);
82 #ifndef _ROAST_INVALID_TEMPLATE_OVERLOAD
83 template <typename _Param, typename _Ret>
84 bool start(_Ret (*func)(_Param), _Param param=0, void* opt=NULL)
86 return start_func(func,param,opt);
90 template <typename _Class, typename _Param>
91 bool start(_Class *ptr, _Param param=0, void* opt=NULL)
93 return start_class_ptr(ptr,param,opt);
96 template <typename _Class, typename _Param, typename _Ret>
97 bool start(_Ret (_Class::*func)(_Param), _Param param=0, void* opt=NULL)
99 // The second parameter is a class pointer or the method should be static.
100 The_second_parameter_is_a_class_pointer_or_the_method_should_be_static.
103 template <typename _Class, typename _Param>
104 bool start(_Class &cls, _Param param=0, void* opt=NULL)
106 return start_class(cls,param,opt);
109 template <typename _Class, typename _Param, typename _Ret>
110 bool start(_Class *p_this, _Ret (_Class::*func)(_Param), _Param param=0, void* opt=NULL)
112 return start_class_ptr(p_this,func,param,opt);
116 ////////////////////////////////////////////////////////
118 bool start_func(callback_func_t func, int param=0, void* opt=NULL)
120 m_handle = m_impl.start(func,param,opt);
121 if ( m_handle == _Impl::invalid_handle )
127 template <typename _Param, typename _Ret>
128 bool start_func(_Ret (*func)(_Param), _Param param=0, void* opt=NULL)
130 m_handle = m_impl.start((callback_func_t)func,(void*)param,opt);
131 if ( m_handle == _Impl::invalid_handle )
137 template <typename _Class, typename _Param>
138 bool start_class(_Class &cls, _Param param=0, void* opt=NULL)
140 m_handle = m_impl.start(
141 _thread_class_internal_callback<_Class>,
142 (int)new _thread_start_class_info<_Class>(new _Class(cls), &_Class::operator(), param, true),
144 if ( m_handle == _Impl::invalid_handle )
151 template <typename _Class, typename _Param>
152 bool start_class_ptr(_Class *p_this, _Param param=0, void* opt=NULL)
154 m_handle = m_impl.start(
155 _thread_class_internal_callback<_Class>,
156 (int)new _thread_start_class_info<_Class>(p_this, &_Class::operator(), param, false),
158 if ( m_handle == NULL )
164 template <typename _Class, typename _Param, typename _Ret>
165 bool start_class_ptr(_Ret (_Class::*func)(_Param), _Param param=0, void* opt=NULL)
167 // The second parameter is a class pointer or the method should be static.
168 The_second_parameter_is_a_class_pointer_or_the_method_should_be_static.
172 template <typename _Class, typename _Param, typename _Ret>
173 bool start_class_ptr(_Class *p_this, _Ret (_Class::*func)(_Param), _Param param=0, void* opt=NULL)
175 m_handle = m_impl.start(
176 _thread_class_internal_callback<_Class>,
177 (int)new _thread_start_class_info<_Class>(p_this, func, param, false),
179 if ( m_handle == _Impl::invalid_handle )
185 ////////////////////////////////////////////////////
189 if ( m_handle != NULL )
190 return m_impl.release(m_handle);
195 if ( m_handle != _Impl::invalid_handle )
196 m_impl.release(m_handle);
198 bool join(unsigned int timeout_ms=-1)
200 if ( m_handle == _Impl::invalid_handle )
202 return m_impl.join(m_handle, timeout_ms);
204 bool is_alive() const
206 if ( m_handle == _Impl::invalid_handle )
208 return m_impl.is_alive(m_handle);
212 return m_impl.get_result(m_handle);
215 void* get_native_handle(){ return m_handle; }
218 ///////////////////////////////////////
222 #include "roast/windows/thread_impl.hpp"
225 #endif//__SFJP_ROAST__std__thread__thread_HPP__