summaryrefslogtreecommitdiffstats
path: root/src/OSSupport/IsThread.cpp
blob: 4864ef28b4f4344b9431f46df9d430759a7c66d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

// IsThread.cpp

// 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"
#include "IsThread.h"





////////////////////////////////////////////////////////////////////////////////
// cIsThread:

cIsThread::cIsThread(const AString & a_ThreadName) :
	m_ShouldTerminate(false),
	m_ThreadName(a_ThreadName)
{
}





cIsThread::~cIsThread()
{
	m_ShouldTerminate = true;
	Wait();
}





bool cIsThread::Start(void)
{
	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;
	}
}





void cIsThread::Stop(void)
{
	m_ShouldTerminate = true;
	Wait();
}





bool cIsThread::Wait(void)
{	
	#ifdef LOGD  // ProtoProxy doesn't have LOGD
		LOGD("Waiting for thread %s to finish", m_ThreadName.c_str());
	#endif  // LOGD

	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;
		}
	}

	#ifdef LOGD  // ProtoProxy doesn't have LOGD
		LOGD("Thread %s finished", m_ThreadName.c_str());
	#endif

	return true;
}