forked from PLCnext/CppExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadExampleComponent.cpp
162 lines (127 loc) · 5.53 KB
/
ThreadExampleComponent.cpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/******************************************************************************
*
* Copyright (c) Phoenix Contact GmbH & Co. KG. All rights reserved.
* Licensed under the MIT. See LICENSE file in the project root for full license information.
*
* ThreadExampleComponent.cpp
*
* Created on: 16.05.2019
* Author: Eduard Münz, Oliver Warneke
*
******************************************************************************/
/******************************************************************************/
/* INCLUDES */
/******************************************************************************/
#include "ThreadExampleComponent.hpp"
#include "Arp/Plc/Commons/Esm/ProgramComponentBase.hpp"
#include "ThreadExampleLibrary.hpp"
namespace ThreadExample
{
///
///ThreadExampleComponent Constructor
///
ThreadExampleComponent::ThreadExampleComponent(IApplication& application, const String& name)
: ComponentBase(application, ::ThreadExample::ThreadExampleLibrary::GetInstance(), name, ComponentCategory::Custom)
, programProvider(*this)
, ProgramComponentBase(::ThreadExample::ThreadExampleLibrary::GetInstance().GetNamespace(), programProvider)
// Worker Thread Example
, workerThreadInstance(make_delegate(this, &ThreadExampleComponent::workerThreadBody) , 10000, "WorkerThreadName")
//Commons/Thread Example
, delegateThreadInstance(ThreadSettings("DelegateThreadName", 20, 0, 0),this,&ThreadExampleComponent::delegateThreadBody,(void*)&myparameter)
, staticThreadInstance(ThreadSettings("StaticThreadName", 20, 0, 0),&ThreadExampleComponent::staticThreadBody,(void*)&xStopThread)
{
}
void ThreadExampleComponent::Initialize()
{
// never remove next line
ProgramComponentBase::Initialize();
// subscribe events from the event system (Nm) here
}
void ThreadExampleComponent::LoadConfig()
{
// load project config here
}
void ThreadExampleComponent::SetupConfig()
{
// never remove next line
ProgramComponentBase::SetupConfig();
// setup project config here
}
void ThreadExampleComponent::ResetConfig()
{
// never remove next line
ProgramComponentBase::ResetConfig();
// implement this inverse to SetupConfig() and LoadConfig()
}
void ThreadExampleComponent::Start(void) {
xStopThread = false;
Log::Info("-------------------------------workerThreadInstance start");
workerThreadInstance.Start();
Log::Info("-------------------------------workerThreadInstance started");
Log::Info("-------------------------------delegateThreadInstance start");
delegateThreadInstance.Start();
Log::Info("-------------------------------delegateThreadInstance started");
Log::Info("-------------------------------staticThreadInstance start");
staticThreadInstance.Start();
Log::Info("-------------------------------staticThreadInstance started");
}
void ThreadExampleComponent::Stop(void) {
// if you want to stop some loops of your thread during execution
// add something like "stoptheThread" before executing workerThreadStop.
xStopThread = true;
Log::Info("-------------------------------workerThreadInstance stop");
workerThreadInstance.Stop();
Log::Info("-------------------------------workerThreadInstance stopped");
Log::Info("-------------------------------staticThreadInstance stop");
staticThreadInstance.Interrupt();
if (staticThreadInstance.IsJoinable()){
staticThreadInstance.Join();
}
Log::Info("-------------------------------staticThreadInstance stopped");
Log::Info("-------------------------------delegateThreadInstance stop");
delegateThreadInstance.Interrupt();
if (delegateThreadInstance.IsJoinable()){
delegateThreadInstance.Join();
}
Log::Info("-------------------------------delegateThreadInstance stopped");
}
/// Thread Body
void ThreadExampleComponent::workerThreadBody(void) {
if(!xStopThread)
{
if(iCountervalue < iEndValue)
iCountervalue ++;
else
iCountervalue = iStartValue;
Log::Info("-------------------------------workerThreadInstance is running, iCountervalue={0}", iCountervalue);
}
else
Log::Info("-------------------------------workerThreadInstance is stopped, iCountervalue={0}", iCountervalue);
}
void ThreadExampleComponent::delegateThreadBody(void* pParameter) {
// has access to this*
int *i = reinterpret_cast<int*>(pParameter);
while(!xStopThread)
{
if(this->myparameter == *i)
Log::Info("-------------------------------ThreadExampleComponent::delegateThreadBody is running successful i={0} , myparameter = {1}", *i, this->myparameter);
else
Log::Info("-------------------------------ThreadExampleComponent::delegateThreadBody is running with ERROR: i={0} , myparameter = {1}", *i, this->myparameter);
Thread::Sleep(1000);
}
Log::Info("-------------------------------ThreadExampleComponent::delegateThreadBody stopped");
}
void ThreadExampleComponent::staticThreadBody(void* pParameter) {
bool* pValue = (bool*) pParameter;
// has NO access to this*
while(!*pValue){
//Do something and sleep for specifiedTime.
Log::Info("-------------------------------ThreadExampleComponent::staticThreadBody is running pParameter={0}", (int) *pValue);
Thread::Sleep(500);
}
Log::Info("-------------------------------ThreadExampleComponent::staticThreadBody stopped");
}
int ThreadExampleComponent::GetCounterValue(){
return iCountervalue;
}
} // end of namespace ThreadExample