thread.h

Go to the documentation of this file.
00001 /*
00002 **  ClanLib SDK
00003 **  Copyright (c) 1997-2011 The ClanLib Team
00004 **
00005 **  This software is provided 'as-is', without any express or implied
00006 **  warranty.  In no event will the authors be held liable for any damages
00007 **  arising from the use of this software.
00008 **
00009 **  Permission is granted to anyone to use this software for any purpose,
00010 **  including commercial applications, and to alter it and redistribute it
00011 **  freely, subject to the following restrictions:
00012 **
00013 **  1. The origin of this software must not be misrepresented; you must not
00014 **     claim that you wrote the original software. If you use this software
00015 **     in a product, an acknowledgment in the product documentation would be
00016 **     appreciated but is not required.
00017 **  2. Altered source versions must be plainly marked as such, and must not be
00018 **     misrepresented as being the original software.
00019 **  3. This notice may not be removed or altered from any source distribution.
00020 **
00021 **  Note: Some of the libraries ClanLib may link to may have additional
00022 **  requirements or restrictions.
00023 **
00024 **  File Author(s):
00025 **
00026 **    Magnus Norddahl
00027 */
00028 
00031 
00032 #pragma once
00033 
00034 
00035 #include "../api_core.h"
00036 #include "sharedptr.h"
00037 #include "runnable.h"
00038 #include "exception.h"
00039 
00040 class CL_Thread_Impl;
00041 
00045 class CL_API_CORE CL_Thread
00046 {
00049 
00050 public:
00052         CL_Thread();
00053 
00054         ~CL_Thread();
00055 
00059 
00060 public:
00061 
00065 
00066 public:
00068         void start(CL_Runnable *runnable);
00069 
00070         template<class C>
00071         void start(C *instance, void (C::*member)())
00072         {
00073                 CL_Runnable *r = new CL_RunnableMember_v0<C>(instance, member);
00074                 try
00075                 {
00076                         start(r);
00077                 }
00078                 catch (const CL_Exception&)
00079                 {
00080                         delete r;
00081                         throw;
00082                 }
00083         }
00084 
00085         template<class C, class P1>
00086         void start(C *instance, void (C::*member)(P1 p1), P1 p1)
00087         {
00088                 CL_Runnable *r = new CL_RunnableMember_v1<C, P1>(instance, member, p1);
00089                 try
00090                 {
00091                         start(r);
00092                 }
00093                 catch (const CL_Exception&)
00094                 {
00095                         delete r;
00096                         throw;
00097                 }
00098         }
00099 
00100         template<class C, class P1, class P2>
00101         void start(C *instance, void (C::*member)(P1 p1, P2 p2), P1 p1, P2 p2)
00102         {
00103                 CL_Runnable *r = new CL_RunnableMember_v2<C, P1, P2>(instance, member, p1, p2);
00104                 try
00105                 {
00106                         start(r);
00107                 }
00108                 catch (const CL_Exception&)
00109                 {
00110                         delete r;
00111                         throw;
00112                 }
00113         }
00114 
00115         template<class C, class P1, class P2, class P3>
00116         void start(C *instance, void (C::*member)(P1 p1, P2 p2, P3 p3), P1 p1, P2 p2, P3 p3)
00117         {
00118                 CL_Runnable *r = new CL_RunnableMember_v3<C, P1, P2, P3>(instance, member, p1, p2, p3);
00119                 try
00120                 {
00121                         start(r);
00122                 }
00123                 catch (const CL_Exception&)
00124                 {
00125                         delete r;
00126                         throw;
00127                 }
00128         }
00129 
00130         template<class C, class P1, class P2, class P3, class P4>
00131         void start(C *instance, void (C::*member)(P1 p1, P2 p2, P3 p3, P4 p4), P1 p1, P2 p2, P3 p3, P4 p4)
00132         {
00133                 CL_Runnable *r = new CL_RunnableMember_v4<C, P1, P2, P3, P4>(instance, member, p1, p2, p3, p4);
00134                 try
00135                 {
00136                         start(r);
00137                 }
00138                 catch (const CL_Exception&)
00139                 {
00140                         delete r;
00141                         throw;
00142                 }
00143         }
00144 
00145         template<class C, class P1, class P2, class P3, class P4, class P5>
00146         void start(C *instance, void (C::*member)(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5), P1 p1, P2 p2, P3 p3, P4 p4, P5 p5)
00147         {
00148                 CL_Runnable *r = new CL_RunnableMember_v5<C, P1, P2, P3, P4, P5>(instance, member, p1, p2, p3, p4, p5);
00149                 try
00150                 {
00151                         start(r);
00152                 }
00153                 catch (const CL_Exception&)
00154                 {
00155                         delete r;
00156                         throw;
00157                 }
00158         }
00159 
00161         void join();
00162 
00169         void kill();
00170 
00172 
00173         static void set_thread_name(const char *name);
00174 
00178 
00179 private:
00180         CL_SharedPtr<CL_Thread_Impl> impl;
00182 };
00183