added WaitThread, removed unused detach()

This commit is contained in:
john30
2015-02-07 14:25:09 +01:00
parent 3133cfd2ad
commit bc1fa51747
2 changed files with 85 additions and 28 deletions
+42 -17
View File
@@ -31,7 +31,7 @@ void* Thread::runThread(void* arg)
Thread::~Thread()
{
if (m_started == true && m_detached == false)
if (m_started == true)
pthread_detach(m_threadid);
if (m_started == true)
@@ -66,7 +66,6 @@ bool Thread::join()
result = pthread_join(m_threadid, NULL);
if (result == 0) {
m_detached = false;
m_started = false;
}
}
@@ -74,23 +73,49 @@ bool Thread::join()
return result == 0;
}
bool Thread::detach()
{
int result = -1;
if (m_started == true && m_detached == false) {
result = pthread_detach(m_threadid);
if (result == 0)
m_detached = true;
}
return result == 0;
}
void Thread::enter() {
m_running = true;
run();
m_running = false;
}
WaitThread::WaitThread()
: Thread()
{
pthread_mutex_init(&m_mutex, NULL);
pthread_cond_init(&m_cond, NULL);
}
WaitThread::~WaitThread()
{
pthread_mutex_destroy(&m_mutex);
pthread_cond_destroy(&m_cond);
}
void WaitThread::stop()
{
pthread_mutex_lock(&m_mutex);
pthread_cond_signal(&m_cond);
pthread_mutex_unlock(&m_mutex);
Thread::stop();
}
bool WaitThread::join()
{
pthread_mutex_lock(&m_mutex);
pthread_cond_signal(&m_cond);
pthread_mutex_unlock(&m_mutex);
return Thread::join();
}
bool WaitThread::Wait(int seconds)
{
struct timespec t;
clock_gettime(CLOCK_REALTIME, &t);
t.tv_sec += seconds;
pthread_mutex_lock(&m_mutex);
pthread_cond_timedwait(&m_cond, &m_mutex, &t);
pthread_mutex_unlock(&m_mutex);
return isRunning();
}
+43 -11
View File
@@ -1,5 +1,6 @@
/*
* Copyright (C) Roland Jax 2012-2014 <ebusd@liwest.at>
* Copyright (C) Roland Jax 2012-2014 <ebusd@liwest.at>,
* John Baier 2014-2015 <ebusd@johnm.de>
*
* This file is part of ebusd.
*
@@ -34,7 +35,7 @@ public:
/**
* constructor.
*/
Thread() : m_threadid(0), m_started(false), m_running(false), m_stopped(false), m_detached(false) {}
Thread() : m_threadid(0), m_started(false), m_running(false), m_stopped(false) {}
/**
* virtual destructor.
@@ -72,17 +73,11 @@ public:
*/
virtual bool join();
/**
* Detach the thread.
* @return whether the thread was detached.
*/
virtual bool detach();
/**
* Get the thread id.
* @return the thread id.
*/
pthread_t self() {return m_threadid; }
pthread_t self() { return m_threadid; }
protected:
@@ -110,8 +105,45 @@ private:
/** Whether the thread was stopped by @a stop() or @a join(). */
bool m_stopped;
/** Whether the thread was detached */
bool m_detached;
};
/**
* A @a Thread that can be waited on.
*/
class WaitThread : public Thread
{
public:
/**
* Constructor.
*/
WaitThread();
/**
* Destructor.
*/
virtual ~WaitThread();
// @copydoc
virtual void stop();
// @copydoc
virtual bool join();
/**
* Wait for the specified amount of time.
* @param seconds the number of seconds to wait.
* @return true if this @a Thread is till running and not yet stopped.
*/
bool Wait(int seconds);
private:
/** the mutex for waiting. */
pthread_mutex_t m_mutex;
/** the condition for waiting. */
pthread_cond_t m_cond;
};