00001 /* 00002 Copyright 2007 Erez Bibi (erezbibi@users.sourceforge.net) 00003 This file is part of Beesnest. 00004 00005 Beesnest is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 Beesnest is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with Beesnest; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00018 */ 00019 00020 /* DcThread.h: interface for the CdcThread class. */ 00021 00022 00023 #ifndef __CDC_THREAD 00024 #define __CDC_THREAD 00025 00026 #include "../DcGlobals.h" 00027 #include "../DcException.h" 00028 00029 enum t_thread_priority 00030 { 00031 DC_TP_LOWER = -2, 00032 DC_TP_LOW = -1, 00033 DC_TP_NORNAL = 0, 00034 DC_TP_HIGH = 1, 00035 DC_TP_HIGHER = 2 00036 }; 00037 00038 /** 00039 * A wrapper on an OS threads. 00040 * 00041 * Note: This class does not terminate the thread in the Destructor, or in any 00042 * other way. The thread terminates when it's start point function exits. 00043 * If your main thread function is running in an infinite loop, you have to 00044 * signal it to exit when you want the thread to terminate! 00045 * <p> 00046 * @author Erez Bibi 00047 * @version 1.0 00048 */ 00049 class CdcThread 00050 { 00051 private: 00052 HANDLE pThread; /* the thread ID */ 00053 int (*pFunc) (void*); /* the real main function. */ 00054 void* pData; /* Pointer to the data to pass to main function. */ 00055 00056 public: 00057 00058 /** Constructor - just initialize the thread pointer. */ 00059 CdcThread () : pThread(NULL), pData(NULL) {} 00060 00061 /** 00062 * Call this function to start a new thread. 00063 * <p> 00064 * @param func Pointer to the main thread function. 00065 * @param pdata Pointer to the data to pass to the thread (can be NULL). 00066 * @param priority The thread priority of execution. The real priority is 00067 * depending on the operation system, but on both we will get similar 00068 * results. 00069 * @param stack_size The new thread stack size in one K units. Default is 00070 * the creating thread stack size. 00071 * @throw (CdcException) If cannot start a new thread. 00072 */ 00073 void MakeThread (int (*pfunc) (void*), void* pdata, int stack_size=0) 00074 throw (CdcException); 00075 00076 /** 00077 * Just close the handle to this thread!!! 00078 */ 00079 void CloseThread (); 00080 00081 00082 /** 00083 * Function to set the priority of the thread. 00084 * <p> 00085 * @param priority The new priority for this thread. 00086 * @return (bool) True on success, False if failed. 00087 **/ 00088 bool SetPriority (t_thread_priority priority); 00089 00090 /** 00091 * Returns True if thread is still active 00092 * <p> 00093 * @return (bool) True if thread function is still running. 00094 */ 00095 bool IsStillActive (); 00096 00097 /** 00098 * Destructor 00099 */ 00100 virtual ~CdcThread (); 00101 }; 00102 00103 00104 #endif /* __CDC_THREAD */ 00105