• Main Page
  • Classes
  • Files
  • File List
  • File Members

Beesnest/Windows/DcThread.cpp

Go to the documentation of this file.
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 /*
00021  * DcThread.cpp: implementation of the CdcThread class.
00022  * for documentation see the header file.
00023  */
00024 
00025 #include "DcThread.h"
00026 
00027 CdcThread::~CdcThread ()
00028 {
00029     CloseThread ();
00030 }
00031 
00032 void CdcThread::MakeThread (int (*pfunc) (void*), void* pdata, int stack_size)
00033      throw (CdcException)
00034 {
00035     pFunc = pfunc;
00036     pData = pdata;
00037 
00038     DWORD ThreadId; /* I dont need it... */
00039 
00040     if (IsStillActive ())
00041         throw CdcException ("Thread is already running");
00042 
00043     pThread = CreateThread (NULL, stack_size * 1024,
00044                             (LPTHREAD_START_ROUTINE) pFunc, (void*) pData,
00045                             0, &ThreadId);
00046 
00047     if (pThread == NULL)
00048         throw CdcException ("Cannot create thread");
00049 }
00050 
00051 
00052 void CdcThread::CloseThread ()
00053 {
00054     if (pThread)
00055         CloseHandle (pThread);
00056     pThread = NULL;
00057 }
00058 
00059 bool CdcThread::SetPriority (t_thread_priority priority)
00060 {
00061     int ret;
00062 
00063     switch (priority)
00064     {
00065         case DC_TP_LOWER :
00066             ret = SetThreadPriority (pThread, THREAD_PRIORITY_LOWEST);
00067             break;
00068         case DC_TP_LOW :
00069             ret = SetThreadPriority (pThread, THREAD_PRIORITY_BELOW_NORMAL);
00070             break;
00071         case DC_TP_NORNAL :
00072             ret = SetThreadPriority (pThread, THREAD_PRIORITY_NORMAL);
00073             break;
00074         case DC_TP_HIGH :
00075             ret = SetThreadPriority (pThread, THREAD_PRIORITY_ABOVE_NORMAL);
00076             break;
00077         case DC_TP_HIGHER :
00078             ret = SetThreadPriority (pThread, THREAD_PRIORITY_HIGHEST);
00079             break;
00080     }
00081     return (ret != 0);
00082 }
00083 
00084 bool CdcThread::IsStillActive ()
00085 {
00086     if (!pThread) return false;
00087     unsigned long code;
00088     GetExitCodeThread (pThread, &code);
00089     return code == STILL_ACTIVE;
00090 }

Generated on Mon Oct 11 2010 16:23:25 for Beesnest by  doxygen 1.7.2