⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 _command_8h-source.html

📁 用vc++写的一个简单的线程池的代码
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<a name="l00078"></a>00078 <span class="comment">//----------------------------------------------------------------------------------</span><a name="l00079"></a>00079 <span class="comment">//a helper function to call binder</span><a name="l00080"></a>00080 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> _Func, <span class="keyword">typename</span> _P1,<span class="keyword">typename</span> _arg1,<span class="keyword">typename</span> _arg2,<span class="keyword">typename</span> _arg3&gt;<a name="l00081"></a><a class="code" href="namespace_plugin_thread_pool.html#3a6bcc0891525e9f30453dd09a5c24e3">00081</a> <a class="code" href="class_plugin_thread_pool_1_1binder3.html">binder3&lt;_Func, _P1, _arg1, _arg2, _arg3&gt;</a> <a class="code" href="namespace_plugin_thread_pool.html#3a6bcc0891525e9f30453dd09a5c24e3">bind3</a>(_Func func, _P1 p1,_arg1 i,_arg2 j,_arg3 k)<a name="l00082"></a>00082 {<a name="l00083"></a>00083       <span class="keywordflow">return</span> <a class="code" href="class_plugin_thread_pool_1_1binder3.html">binder3&lt;_Func, _P1, _arg1, _arg2, _arg3&gt;</a> (func, p1,i,j,k);<a name="l00084"></a>00084 }<a name="l00085"></a>00085 <span class="comment">//--------------------------------------------------------</span><a name="l00086"></a>00086 <span class="comment">//a binder class for function with signature - int fun(void)</span><a name="l00087"></a>00087 <a name="l00088"></a>00088 <span class="keyword">template</span>&lt;<span class="keyword">typename</span> _Func,<span class="keyword">typename</span> _Ptr&gt;<a name="l00089"></a><a class="code" href="class_plugin_thread_pool_1_1binder.html">00089</a> <span class="keyword">class </span><a class="code" href="class_plugin_thread_pool_1_1binder.html">binder</a><a name="l00090"></a>00090 {<a name="l00091"></a>00091 <span class="keyword">public</span>:<a name="l00092"></a>00092         <span class="comment">//This is the  constructor that does the binding part</span><a name="l00093"></a><a class="code" href="class_plugin_thread_pool_1_1binder.html#55a3ff883238e5ce2ab9bbdca3cedfd2">00093</a>         <a class="code" href="class_plugin_thread_pool_1_1binder.html#55a3ff883238e5ce2ab9bbdca3cedfd2">binder</a>(_Func fn,_Ptr ptr)<a name="l00094"></a>00094                 :<a class="code" href="class_plugin_thread_pool_1_1binder.html#d02377fa96f8e2b62e8e638f2bb5c69c">m_ptr</a>(ptr),<a class="code" href="class_plugin_thread_pool_1_1binder.html#bb85c5274043edd4c1d7d4e408aadb18">m_fn</a>(fn){}<a name="l00095"></a>00095 <a name="l00096"></a>00096         <span class="comment">//and this is the function object that class it</span><a name="l00097"></a><a class="code" href="class_plugin_thread_pool_1_1binder.html#b746a2b18a3d306254ec145e1abdc607">00097</a>         <span class="keywordtype">void</span> <a class="code" href="class_plugin_thread_pool_1_1binder.html#b746a2b18a3d306254ec145e1abdc607">operator() </a>()<span class="keyword"> const</span><a name="l00098"></a>00098 <span class="keyword">        </span>{<a name="l00099"></a>00099                   <a class="code" href="class_plugin_thread_pool_1_1binder.html#bb85c5274043edd4c1d7d4e408aadb18">m_fn</a>(<a class="code" href="class_plugin_thread_pool_1_1binder.html#d02377fa96f8e2b62e8e638f2bb5c69c">m_ptr</a>);<a name="l00100"></a>00100         }<a name="l00101"></a>00101         <a name="l00102"></a>00102 <span class="keyword">private</span>:<a name="l00103"></a><a class="code" href="class_plugin_thread_pool_1_1binder.html#d02377fa96f8e2b62e8e638f2bb5c69c">00103</a>         _Ptr <a class="code" href="class_plugin_thread_pool_1_1binder.html#d02377fa96f8e2b62e8e638f2bb5c69c">m_ptr</a>;<a name="l00104"></a><a class="code" href="class_plugin_thread_pool_1_1binder.html#bb85c5274043edd4c1d7d4e408aadb18">00104</a>         _Func <a class="code" href="class_plugin_thread_pool_1_1binder.html#bb85c5274043edd4c1d7d4e408aadb18">m_fn</a>;<a name="l00105"></a>00105 <a name="l00106"></a>00106 };<a name="l00107"></a>00107 <a name="l00108"></a>00108 <span class="comment">//a helper function to call binder</span><a name="l00109"></a>00109 <a name="l00110"></a>00110 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> _Func, <span class="keyword">typename</span> _P1&gt;<a name="l00111"></a><a class="code" href="namespace_plugin_thread_pool.html#e5f9caf7c3e35e580700e00676c81733">00111</a> <a class="code" href="class_plugin_thread_pool_1_1binder.html">binder&lt;_Func, _P1&gt;</a> <a class="code" href="namespace_plugin_thread_pool.html#e5f9caf7c3e35e580700e00676c81733">bind</a>(_Func func, _P1 p1)<a name="l00112"></a>00112 {<a name="l00113"></a>00113       <span class="keywordflow">return</span> <a class="code" href="class_plugin_thread_pool_1_1binder.html">binder&lt;_Func, _P1&gt;</a>(func, p1);<a name="l00114"></a>00114 }<a name="l00115"></a>00115 <a name="l00116"></a>00116 <a name="l00117"></a>00117 <a name="l00118"></a><a class="code" href="namespace_plugin_thread_pool.html#c35e46c31b843129bfc6f8cf4847f6f0">00118</a> <span class="keyword">const</span> <span class="keywordtype">int</span> <a class="code" href="namespace_plugin_thread_pool.html#c35e46c31b843129bfc6f8cf4847f6f0">PRIO_LOW</a> =10;<a name="l00119"></a><a class="code" href="namespace_plugin_thread_pool.html#ce744b8830668fc53d7c97b3c77d7ce4">00119</a> <span class="keyword">const</span> <span class="keywordtype">int</span> <a class="code" href="namespace_plugin_thread_pool.html#ce744b8830668fc53d7c97b3c77d7ce4">PRIO_NORMAL</a> =20;<a name="l00120"></a><a class="code" href="namespace_plugin_thread_pool.html#9c6b32e1e66caf69ea18d7d087983e74">00120</a> <span class="keyword">const</span> <span class="keywordtype">int</span> <a class="code" href="namespace_plugin_thread_pool.html#9c6b32e1e66caf69ea18d7d087983e74">PRIO_HIGH</a> =30;<a name="l00121"></a>00121 <a name="l00122"></a>00122 <a name="l00123"></a>00123 <span class="comment">//***************************************************************************************</span><a name="l00125"></a>00125 <span class="comment"></span><span class="comment">//***************************************************************************************</span><a name="l00126"></a>00126 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;<a name="l00127"></a><a class="code" href="class_plugin_thread_pool_1_1_command.html">00127</a> <span class="keyword">class </span><a class="code" href="class_plugin_thread_pool_1_1_command.html">Command</a><a name="l00128"></a>00128 {<a name="l00129"></a><a class="code" href="class_plugin_thread_pool_1_1_command.html#960a60f376ad7a916bd3c97f11d2a824">00129</a>         T* <a class="code" href="class_plugin_thread_pool_1_1_command.html#960a60f376ad7a916bd3c97f11d2a824">m_objptr</a>; <span class="comment">//pointer to the object</span><a name="l00130"></a>00130         <a name="l00131"></a>00131         <span class="comment">//for a function that takes no argument and void return type</span><a name="l00132"></a><a class="code" href="class_plugin_thread_pool_1_1_command.html#eb0d44a1d64ed5e8ee8b15e069f31e89">00132</a>         void (T::*<a class="code" href="class_plugin_thread_pool_1_1_command.html#7552d1a7c1afc09dcdd886e7ade68789">method</a>)();                                                                                             ;<a name="l00133"></a>00133         <span class="comment">//for a function that takes no argument but non void return type</span><a name="l00134"></a>00134         <span class="keyword">typedef</span> <a class="code" href="class_plugin_thread_pool_1_1binder.html">binder&lt; std::mem_fun_t&lt;int,T&gt;</a>, T* &gt;                                                             <a class="code" href="class_plugin_thread_pool_1_1binder.html">F0</a>; <a name="l00135"></a>00135         <span class="comment">//for a function that takes 1 parameter as argument</span><a name="l00136"></a><a class="code" href="class_plugin_thread_pool_1_1_command.html#85b52dc64e486eac2c2c4d9db92b9b3c">00136</a>         <span class="keyword">typedef</span> std::binder1st&lt; std::mem_fun1_t&lt;int,T,int&gt; &gt;                                    <a class="code" href="class_plugin_thread_pool_1_1_command.html#85b52dc64e486eac2c2c4d9db92b9b3c">F1</a>; <a name="l00137"></a>00137         <span class="comment">//for a function that takes three parameter as argumet</span><a name="l00138"></a><a class="code" href="class_plugin_thread_pool_1_1_command.html#0f37693ab1e011ab30fe85abea2c9f00">00138</a>         <span class="keyword">typedef</span> <a class="code" href="class_plugin_thread_pool_1_1binder3.html">binder3&lt;mem_fun3_t&lt;int,T,int,int,int&gt;</a> ,T* ,int,int,<span class="keywordtype">int</span>&gt;                 <a class="code" href="class_plugin_thread_pool_1_1binder3.html">F3</a>;<a name="l00139"></a>00139 <a name="l00140"></a>00140         <span class="comment">// Note this is not the best design , but putting here for illustration</span><a name="l00141"></a>00141         <span class="comment">// the F0, F1 or F3 datatype can itself be templatized</span><a name="l00142"></a>00142 <a name="l00143"></a>00143         <span class="comment">//pointer to the method of the object</span><a name="l00144"></a><a class="code" href="class_plugin_thread_pool_1_1_command.html#09eb6f552921e01ef734cfb42790caa2">00144</a>         <a class="code" href="class_plugin_thread_pool_1_1binder.html">F0</a>* <a class="code" href="class_plugin_thread_pool_1_1_command.html#09eb6f552921e01ef734cfb42790caa2">method0</a>;  <a name="l00145"></a><a class="code" href="class_plugin_thread_pool_1_1_command.html#5aaecab58914d6fca0996d12d5c802b9">00145</a>         <a class="code" href="class_plugin_thread_pool_1_1_command.html#85b52dc64e486eac2c2c4d9db92b9b3c">F1</a>* <a class="code" href="class_plugin_thread_pool_1_1_command.html#5aaecab58914d6fca0996d12d5c802b9">method1</a>;<a name="l00146"></a><a class="code" href="class_plugin_thread_pool_1_1_command.html#e81dab19d4e8944a4c349c220599946e">00146</a>         <a class="code" href="class_plugin_thread_pool_1_1binder3.html">F3</a>* <a class="code" href="class_plugin_thread_pool_1_1_command.html#e81dab19d4e8944a4c349c220599946e">method3</a>;<a name="l00147"></a>00147         <a name="l00148"></a><a class="code" href="class_plugin_thread_pool_1_1_command.html#0813562408adb30c60ad501c8b533b00">00148</a>         <span class="keywordtype">long</span> <a class="code" href="class_plugin_thread_pool_1_1_command.html#0813562408adb30c60ad501c8b533b00">m_timeout</a>;<span class="comment">//the max tolerated time for a task to finish</span><a name="l00149"></a><a class="code" href="class_plugin_thread_pool_1_1_command.html#a79238db30e99d1c03f72050ba48a898">00149</a>         std::string <a class="code" href="class_plugin_thread_pool_1_1_command.html#a79238db30e99d1c03f72050ba48a898">m_key</a>;<a name="l00150"></a><a class="code" href="class_plugin_thread_pool_1_1_command.html#199d3a70de4ff59db4f85f09a9fe0bdf">00150</a>         <span class="keywordtype">long</span> <a class="code" href="class_plugin_thread_pool_1_1_command.html#199d3a70de4ff59db4f85f09a9fe0bdf">m_value</a>;<a name="l00151"></a><a class="code" href="class_plugin_thread_pool_1_1_command.html#03e2cf02a2ff39a8d614d56bd0345682">00151</a>         <span class="keywordtype">int</span> <a class="code" href="class_plugin_thread_pool_1_1_command.html#03e2cf02a2ff39a8d614d56bd0345682">m_par1</a>;<a name="l00152"></a>00152 <a name="l00153"></a>00153 <span class="keyword">public</span>:<a name="l00154"></a>00154 <a name="l00156"></a>00156 <a name="l00157"></a><a class="code" href="class_plugin_thread_pool_1_1_command.html#58aab3290ff60ca553e827432cfd2569">00157</a>         <span class="keyword">explicit</span> <a class="code" href="class_plugin_thread_pool_1_1_command.html#58aab3290ff60ca553e827432cfd2569">Command</a>(T* pObj,<span class="keywordtype">void</span> (T::*p_method)() ,<span class="keywordtype">long</span> timeout,<span class="keyword">const</span> <span class="keywordtype">char</span>* key,<span class="keywordtype">long</span> priority = <a class="code" href="namespace_plugin_thread_pool.html#ce744b8830668fc53d7c97b3c77d7ce4">PRIO_NORMAL</a> )<a name="l00158"></a>00158                 :<a class="code" href="class_plugin_thread_pool_1_1_command.html#960a60f376ad7a916bd3c97f11d2a824">m_objptr</a>(pObj),<a class="code" href="class_plugin_thread_pool_1_1_command.html#0813562408adb30c60ad501c8b533b00">m_timeout</a>(timeout),<a class="code" href="class_plugin_thread_pool_1_1_command.html#a79238db30e99d1c03f72050ba48a898">m_key</a>(key),<a class="code" href="class_plugin_thread_pool_1_1_command.html#199d3a70de4ff59db4f85f09a9fe0bdf">m_value</a>(priority),<a class="code" href="class_plugin_thread_pool_1_1_command.html#09eb6f552921e01ef734cfb42790caa2">method0</a>(0),<a class="code" href="class_plugin_thread_pool_1_1_command.html#5aaecab58914d6fca0996d12d5c802b9">method1</a>(0),<a class="code" href="class_plugin_thread_pool_1_1_command.html#e81dab19d4e8944a4c349c220599946e">method3</a>(0)<a name="l00159"></a>00159         {<a name="l00160"></a>00160                 <a name="l00161"></a>00161                 <a class="code" href="class_plugin_thread_pool_1_1_command.html#7552d1a7c1afc09dcdd886e7ade68789">method</a> = p_method;<a name="l00162"></a>00162         }<a name="l00163"></a>00163 <a name="l00164"></a>00164 <a name="l00165"></a>00165 <a name="l00166"></a><a class="code" href="class_plugin_thread_pool_1_1_command.html#eb21ebe43b54445c85844a6767c4b1c9">00166</a>         <span class="keyword">explicit</span> <a class="code" href="class_plugin_thread_pool_1_1_command.html#58aab3290ff60ca553e827432cfd2569">Command</a>(T* pObj,<a class="code" href="class_plugin_thread_pool_1_1binder.html">F0</a>* p_method,<span class="keywordtype">long</span> timeout,<span class="keyword">const</span> <span class="keywordtype">char</span>* key,<span class="keywordtype">long</span> priority = <a class="code" href="namespace_plugin_thread_pool.html#ce744b8830668fc53d7c97b3c77d7ce4">PRIO_NORMAL</a> ):<a name="l00167"></a>00167                                 <a class="code" href="class_plugin_thread_pool_1_1_command.html#960a60f376ad7a916bd3c97f11d2a824">m_objptr</a>(pObj),<a class="code" href="class_plugin_thread_pool_1_1_command.html#0813562408adb30c60ad501c8b533b00">m_timeout</a>(timeout),<a class="code" href="class_plugin_thread_pool_1_1_command.html#a79238db30e99d1c03f72050ba48a898">m_key</a>(key),<a class="code" href="class_plugin_thread_pool_1_1_command.html#199d3a70de4ff59db4f85f09a9fe0bdf">m_value</a>(priority),<a class="code" href="class_plugin_thread_pool_1_1_command.html#5aaecab58914d6fca0996d12d5c802b9">method1</a>(0),<a class="code" href="class_plugin_thread_pool_1_1_command.html#e81dab19d4e8944a4c349c220599946e">method3</a>(0),<a class="code" href="class_plugin_thread_pool_1_1_command.html#7552d1a7c1afc09dcdd886e7ade68789">method</a>(0)<a name="l00168"></a>00168         {<a name="l00169"></a>00169                 <a name="l00170"></a>00170                 <a class="code" href="class_plugin_thread_pool_1_1_command.html#09eb6f552921e01ef734cfb42790caa2">method0</a> = p_method;<a name="l00171"></a>00171         }<a name="l00172"></a>00172 <a name="l00173"></a>00173         <span class="comment">//just to illustrate the usage with a method signature taking one parameter</span><a name="l00174"></a><a class="code" href="class_plugin_thread_pool_1_1_command.html#261bc0b38f8c4f79676d987da62f3202">00174</a>         <span class="keyword">explicit</span> <a class="code" href="class_plugin_thread_pool_1_1_command.html#58aab3290ff60ca553e827432cfd2569">Command</a>(T* pObj,<a class="code" href="class_plugin_thread_pool_1_1_command.html#85b52dc64e486eac2c2c4d9db92b9b3c">F1</a>* p_method,<span class="keywordtype">int</span> par1,<span class="keywordtype">long</span> timeout,<span class="keyword">const</span> <span class="keywordtype">char</span>* key,<span class="keywordtype">long</span> priority = <a class="code" href="namespace_plugin_thread_pool.html#ce744b8830668fc53d7c97b3c77d7ce4">PRIO_NORMAL</a> ):<a name="l00175"></a>00175                                 <a class="code" href="class_plugin_thread_pool_1_1_command.html#960a60f376ad7a916bd3c97f11d2a824">m_objptr</a>(pObj),<a class="code" href="class_plugin_thread_pool_1_1_command.html#0813562408adb30c60ad501c8b533b00">m_timeout</a>(timeout),<a class="code" href="class_plugin_thread_pool_1_1_command.html#a79238db30e99d1c03f72050ba48a898">m_key</a>(key),<a class="code" href="class_plugin_thread_pool_1_1_command.html#199d3a70de4ff59db4f85f09a9fe0bdf">m_value</a>(priority),<a class="code" href="class_plugin_thread_pool_1_1_command.html#09eb6f552921e01ef734cfb42790caa2">method0</a>(0),<a class="code" href="class_plugin_thread_pool_1_1_command.html#e81dab19d4e8944a4c349c220599946e">method3</a>(0),<a class="code" href="class_plugin_thread_pool_1_1_command.html#7552d1a7c1afc09dcdd886e7ade68789">method</a>(0)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -