2012-06-14 13:06:06 +00:00
|
|
|
|
2012-09-23 21:23:33 +00:00
|
|
|
// IsThread.cpp
|
2012-06-14 13:06:06 +00:00
|
|
|
|
|
|
|
// Implements the cIsThread class representing an OS-independent wrapper for a class that implements a thread.
|
|
|
|
// This class will eventually suupersede the old cThread class
|
|
|
|
|
|
|
|
#include "Globals.h"
|
2012-09-23 21:23:33 +00:00
|
|
|
#include "IsThread.h"
|
2012-06-14 13:06:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// cIsThread:
|
|
|
|
|
2014-10-18 23:29:34 +00:00
|
|
|
cIsThread::cIsThread(const AString & a_ThreadName) :
|
2012-06-14 13:06:06 +00:00
|
|
|
m_ShouldTerminate(false),
|
2014-10-18 23:29:34 +00:00
|
|
|
m_ThreadName(a_ThreadName)
|
2012-06-14 13:06:06 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cIsThread::~cIsThread()
|
|
|
|
{
|
|
|
|
m_ShouldTerminate = true;
|
|
|
|
Wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cIsThread::Start(void)
|
|
|
|
{
|
2014-10-18 23:29:34 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
m_Thread = std::thread(&cIsThread::Execute, this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (std::system_error & a_Exception)
|
|
|
|
{
|
|
|
|
LOGERROR("ERROR: Could not create thread \"%s\", error = %s!", m_ThreadName.c_str(), a_Exception.code(), a_Exception.what());
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-14 13:06:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-11 17:40:15 +00:00
|
|
|
void cIsThread::Stop(void)
|
|
|
|
{
|
|
|
|
m_ShouldTerminate = true;
|
|
|
|
Wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 13:06:06 +00:00
|
|
|
bool cIsThread::Wait(void)
|
2014-10-20 20:26:18 +00:00
|
|
|
{
|
2013-08-25 19:57:12 +00:00
|
|
|
#ifdef LOGD // ProtoProxy doesn't have LOGD
|
|
|
|
LOGD("Waiting for thread %s to finish", m_ThreadName.c_str());
|
|
|
|
#endif // LOGD
|
2012-06-14 13:06:06 +00:00
|
|
|
|
2014-10-18 23:29:34 +00:00
|
|
|
if (m_Thread.joinable())
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m_Thread.join();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (std::system_error & a_Exception)
|
|
|
|
{
|
|
|
|
LOGERROR("ERROR: Could wait for thread \"%s\" to finish, error = %s!", m_ThreadName.c_str(), a_Exception.code(), a_Exception.what());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-06-14 13:06:06 +00:00
|
|
|
|
2014-10-18 23:29:34 +00:00
|
|
|
#ifdef LOGD // ProtoProxy doesn't have LOGD
|
|
|
|
LOGD("Thread %s finished", m_ThreadName.c_str());
|
2012-06-14 13:06:06 +00:00
|
|
|
#endif
|
|
|
|
|
2014-10-18 23:29:34 +00:00
|
|
|
return true;
|
2014-06-08 19:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|