-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
228 lines (183 loc) · 6.06 KB
/
Copy pathMain.cpp
File metadata and controls
228 lines (183 loc) · 6.06 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
////////////////////////////////////////////////////////////////////////
//
// Simple implementation of Observer Design Pattern to implement thread
// class which can send notifications to attached observers when the
// new thread is created status as 'true' with thread ID and status as
// 'false' when the thread execution ends
//
////////////////////////////////////////////////////////////////////////
#include <Windows.h>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class IObserver;
////////////////////////////////////////////////////////////////////////
class ISubject
{
public:
virtual void AttachObserver(IObserver * observerPtr)=0;
virtual void DettachObserver(IObserver * observerPtr)=0;
virtual void Notify(DWORD ThreadID, bool Status)=0;
};
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
class IObserver
{
public:
virtual void AutoAttach(ISubject * subjectPtr)=0;
virtual void AutoDettach(ISubject * subjectPtr)=0;
virtual void Update(DWORD ThreadID, bool Status)=0;
};
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
class CSubject : public ISubject
{
public:
CSubject(){}
virtual ~CSubject(){}
virtual void AttachObserver(IObserver * observerPtr){}
virtual void DettachObserver(IObserver * observerPtr){}
virtual void Notify(DWORD ThreadID, bool Status){}
};
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
class ThreadWraper : public CSubject
{
DWORD m_threadID, m_threadWaitTime;
HANDLE m_threadHandle;
protected:
static vector<IObserver *> ObserverVect;
public:
ThreadWraper(int);
~ThreadWraper();
void AttachObserver(IObserver * observerPtr);
void DettachObserver(IObserver * observerPtr);
void Notify(DWORD ThreadID, bool Status);
//Thread proc
static DWORD WINAPI ThreadProc(LPVOID lpParameter);
HANDLE createThread();
void WaitForThreadToFinish();
};
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
class CObserver : public IObserver
{
public:
CObserver(){}
virtual ~CObserver(){}
virtual void AutoAttach(ISubject * subjectPtr){}
virtual void AutoDettach(ISubject * subjectPtr){}
//Call back function
virtual void Update(DWORD ThreadID, bool Status){}
};
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
class CustomThreadObserver : public CObserver
{
int m_observerID;
static int NumberOfBoservers;
public:
CustomThreadObserver()
{
m_observerID = ++NumberOfBoservers;
}
~CustomThreadObserver(){}
void AutoAttach(ISubject * ptr);
void AutoDettach(ISubject * ptr);
void Update(DWORD ThreadID, bool Status);
};
////////////////////////////////////////////////////////////////////////
//////////////////////////static initializations////////////////////////
vector<IObserver *> ThreadWraper::ObserverVect;
int CustomThreadObserver::NumberOfBoservers=0;
////////////////////////////////////////////////////////////////////////
ThreadWraper::ThreadWraper(int WaitTime = 1000) : m_threadWaitTime(WaitTime)
{
ThreadWraper::ObserverVect.reserve(10);
}
ThreadWraper::~ThreadWraper()
{
if(!ThreadWraper::ObserverVect.empty())
{
ThreadWraper::ObserverVect.clear();
}
}
void ThreadWraper::AttachObserver(IObserver * observerPtr)
{
ThreadWraper::ObserverVect.push_back(observerPtr);
}
void ThreadWraper::DettachObserver(IObserver * observerPtr)
{
vector<IObserver *>::iterator i = find(ThreadWraper::ObserverVect.begin(), ThreadWraper::ObserverVect.end(),observerPtr);
if(i != ThreadWraper::ObserverVect.end())
{
ThreadWraper::ObserverVect.erase(i);
}
}
void ThreadWraper::Notify(DWORD ThreadID, bool Status)
{
for(unsigned i = 0; i < ThreadWraper::ObserverVect.size(); ++i)
{
ThreadWraper::ObserverVect[i]->Update(ThreadID, Status);
}
}
DWORD WINAPI ThreadWraper::ThreadProc(LPVOID lpParam)
{
ThreadWraper * ptr = static_cast<ThreadWraper *>(lpParam);
ptr->Notify(ptr->m_threadID,true);
//wait time for thread execution
Sleep(ptr->m_threadWaitTime);
ptr->Notify(ptr->m_threadID,false);
return 0;
}
HANDLE ThreadWraper::createThread()
{
m_threadHandle = CreateThread(NULL,0,ThreadProc,(LPVOID)this,0,&m_threadID);
return m_threadHandle;
}
void ThreadWraper::WaitForThreadToFinish()
{
if(m_threadHandle != NULL)
{
WaitForSingleObject(m_threadHandle,INFINITE);
}
}
////////////////////////////////////////////////////////////////////////
void CustomThreadObserver::AutoAttach(ISubject * subjectPtr)
{
subjectPtr->AttachObserver(this);
}
void CustomThreadObserver::AutoDettach(ISubject * subjectPtr)
{
subjectPtr->DettachObserver(this);
}
void CustomThreadObserver::Update(DWORD ThreadID, bool Status)
{
if(Status)
{
cout<<"Observer : "<<this->m_observerID<<" : The thread : "<<ThreadID<<" has started execution."<<endl;
}
else
{
cout<<"Observer : "<<this->m_observerID<<" : The thread : "<<ThreadID<<" has stoped execution !"<<endl;
}
}
////////////////////////////////////////////////////////////////////////
int main()
{
/*
====== Steps to use thread observers ======
1] create thredwraper object with wait time
2] create customethreadobserver object then call attach observer on it with address of object created in step 1
3] Then call createThread method on threadWraper object
4] then update call back will come for all observers, attached to that thread
*/
ThreadWraper Thread1(15000);
CustomThreadObserver ThreadObserver1, ThreadObserver2;
ThreadObserver1.AutoAttach(&Thread1);
ThreadObserver2.AutoAttach(&Thread1);
Thread1.createThread();
Thread1.WaitForThreadToFinish();
return 0;
}