added: thead.{cpp,h}

This commit is contained in:
Bob van Loosen 2012-12-19 22:36:42 +01:00
parent 26811b5d99
commit 1f640349c6
3 changed files with 121 additions and 0 deletions

70
src/util/thread.cpp Normal file
View file

@ -0,0 +1,70 @@
/*
* boblight
* Copyright (C) Bob 2009
*
* boblight is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* boblight is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "thread.h"
CThread::CThread()
{
m_running = false;
}
CThread::~CThread()
{
StopThread();
}
void CThread::StartThread()
{
m_stop = false;
m_running = true;
pthread_create(&m_thread, NULL, ThreadFunction, reinterpret_cast<void*>(this));
}
void* CThread::ThreadFunction(void* args)
{
CThread* thread = reinterpret_cast<CThread*>(args);
thread->Process();
thread->m_running = false;
}
void CThread::Process()
{
}
void CThread::StopThread()
{
AsyncStopThread();
JoinThread();
}
void CThread::AsyncStopThread()
{
m_stop = true;
}
void CThread::JoinThread()
{
if (m_running)
pthread_join(m_thread, NULL);
}
bool CThread::IsRunning()
{
return m_running;
}

50
src/util/thread.h Normal file
View file

@ -0,0 +1,50 @@
/*
* boblight
* Copyright (C) Bob 2009
*
* boblight is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* boblight is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CTHREAD
#define CTHREAD
#define __STDC_CONSTANT_MACROS
#define __STDC_LIMIT_MACROS
#include <stdint.h>
#include <pthread.h>
#include <unistd.h>
class CThread
{
public:
CThread();
~CThread();
void StartThread();
void StopThread();
void AsyncStopThread();
void JoinThread();
bool IsRunning();
protected:
pthread_t m_thread;
volatile bool m_stop;
volatile bool m_running;
static void* ThreadFunction(void* args);
virtual void Process();
};
#endif //CTHREAD

View file

@ -42,6 +42,7 @@ def build(bld):
src/util/timeutils.cpp\
src/util/condition.cpp\
src/util/tcpsocket.cpp\
src/util/thread.cpp\
src/fft.cpp',
use=['m','pthread','rt', 'jack', 'fftw3', 'fftw3f', 'samplerate'],
includes='./src',