5 #ifndef __SFJP_ROAST__std__thread__thread_HPP__
6 #define __SFJP_ROAST__std__thread__thread_HPP__
8 //#include "roast/_common.hpp"
11 /*typedef int (*thread_func_t)(int);
12 typedef int (*thread_callback_t)(int);
13 typedef void* thread_handle_t;*/
17 typedef int (*thread_func_t)(int);
18 typedef int (*thread_callback_t)(int);
19 typedef void* thread_handle_t;
21 ///////////////////////////////////////////////////////
23 class thread_exception : public ::std::string
26 thread_exception(const char* msg) : ::std::string(msg) {}
27 thread_exception(const ::std::string &s) : ::std::string(s) {}
30 /////////////////////////////////////////////////////////////////
32 template <typename _Class, typename _Param, typename _Ret>
33 struct _thread_start_class_info
35 typedef _Ret (_Class::*callback_t)(_Param);
40 bool need_instance_delete;
42 _thread_start_class_info(_Class *instance_in, callback_t func_in,
43 _Param param_in, bool need_instance_delete_in)
45 instance(instance_in), func(func_in),
46 param(param_in), need_instance_delete(need_instance_delete_in) {}
49 template <typename _Class>
50 int _thread_class_internal_callback(int start_class_info_ptr)
52 _thread_start_class_info<_Class>* p = (_thread_start_class_info<_Class>*)start_class_info_ptr;
53 int ret = ((p->instance)->*p->func)(p->param);
54 if ( p->need_instance_delete )
60 ////////////////////////////////////////////////////////////
62 template <typename _Impl>
66 typedef int (*func_t)(int);
67 typedef int (*callback_t)(int);
68 typedef void* handle_t;
71 thread_handle_t m_handle;
77 /*thread_(thread_func_t func, int param=0, void* opt=NULL)
79 if ( start(func,param,opt) != true )
80 thread_exception("thread start failed.");
82 template <typename _Func, typename _Param>
83 thread_(_Func func, _Param param=0, void* opt=NULL)
85 if ( start(func,param,opt) != true )
86 thread_exception("thread start failed.");
89 //#ifndef _ROAST_INVALID_TEMPLATE_OVERLOAD
90 template <typename _Class, typename _Param, typename _Ret>
91 thread_(_Class* p_this, _Ret (_Class::*func)(_Param), _Param param=0, void* opt=NULL)
93 if ( start_class_ptr(p_this,func,param,opt) != true )
94 thread_exception("thread start failed.");
102 ////////////////////////////////////////////////////////
104 bool start(thread_func_t func, int param=0, void* opt=NULL)
106 return start_func(func,param,opt);
109 #ifndef _ROAST_INVALID_TEMPLATE_OVERLOAD
110 template <typename _Param, typename _Ret>
111 bool start(_Ret (*func)(_Param), _Param param=0, void* opt=NULL)
113 return start_func(func,param,opt);
117 template <typename _Class, typename _Param>
118 bool start(_Class *ptr, _Param param=0, void* opt=NULL)
120 return start_class_ptr(ptr,param,opt);
123 template <typename _Class, typename _Param, typename _Ret>
124 bool start(_Ret (__thiscall _Class::*func)(_Param), _Param param=0, void* opt=NULL)
126 // The second parameter is a class pointer or the method should be static.
127 The_second_parameter_is_a_class_pointer_or_the_method_should_be_static.
130 template <typename _Class, typename _Param>
131 bool start(_Class &cls, _Param param=0, void* opt=NULL)
133 return start_class(cls,param,opt);
136 template <typename _Class, typename _Param, typename _Ret>
137 bool start(_Class *p_this, _Ret (_Class::*func)(_Param), _Param param=0, void* opt=NULL)
139 return start_class_ptr(p_this,func,param,opt);
143 ////////////////////////////////////////////////////////
145 bool start_func(thread_func_t func, int param=0, void* opt=NULL)
147 m_handle = m_impl.start(func,param,opt);
148 if ( m_handle == NULL )
154 template <typename _Param, typename _Ret>
155 bool start_func(_Ret (*func)(_Param), _Param param=0, void* opt=NULL)
157 m_handle = m_impl.start((thread_func_t)func,(int)param,opt);
158 if ( m_handle == NULL )
164 template <typename _Class, typename _Param>
165 bool start_class(_Class &cls, _Param param=0, void* opt=NULL)
167 m_handle = m_impl.start(
168 _thread_class_internal_callback<_Class>,
169 (int)new _thread_start_class_info<_Class>(new _Class(cls), &_Class::operator(), param, true),
171 if ( m_handle == NULL )
178 template <typename _Class, typename _Param>
179 bool start_class_ptr(_Class *p_this, _Param param=0, void* opt=NULL)
181 m_handle = m_impl.start(
182 _thread_class_internal_callback<_Class>,
183 (int)new _thread_start_class_info<_Class>(p_this, &_Class::operator(), param, false),
185 if ( m_handle == NULL )
191 template <typename _Class, typename _Param, typename _Ret>
192 bool start_class_ptr(_Ret (_Class::*func)(_Param), _Param param=0, void* opt=NULL)
194 // The second parameter is a class pointer or the method should be static.
195 The_second_parameter_is_a_class_pointer_or_the_method_should_be_static.
199 template <typename _Class, typename _Param, typename _Ret>
200 bool start_class_ptr(_Class *p_this, _Ret (_Class::*func)(_Param), _Param param=0, void* opt=NULL)
202 m_handle = m_impl.start(
203 _thread_class_internal_callback<_Class>,
204 (int)new _thread_start_class_info<_Class>(p_this, func, param, false),
206 if ( m_handle == NULL )
212 ////////////////////////////////////////////////////
216 if ( m_handle != NULL )
217 return m_impl.release(m_handle);
220 bool join(unsigned int timeout_ms=-1)
222 if ( m_handle == NULL )
224 return m_impl.join(m_handle, timeout_ms);
228 if ( m_handle == NULL )
230 return m_impl.is_alive(m_handle);
234 return m_impl.get_result(m_handle);
237 void* get_native_handle(){ return m_handle; }
240 ///////////////////////////////////////
244 #include "roast/windows/thread_impl.hpp"
247 #endif//__SFJP_ROAST__std__thread__thread_HPP__