-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDummyReciver.cpp
More file actions
78 lines (69 loc) · 1.87 KB
/
DummyReciver.cpp
File metadata and controls
78 lines (69 loc) · 1.87 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
//
// DummyReciver.cpp
// ThreadPool
//
// Created by Andrew Rose on 24.07.13.
// Copyright (c) 2013 Andrew Rose. All rights reserved.
//
#include <functional>
#include <algorithm>
#include "DummyReciver.h"
using namespace std;
DummyReciver::~DummyReciver()
{
if(!newSignalCondition)delete newSignalCondition;
if(!queLock)delete queLock;
}
void DummyReciver::processSignal(const string& Signal)
{
std::chrono::milliseconds dura(2);
std::this_thread::sleep_for(dura);
std::cout<<Signal<<std::endl;
}
const bool DummyReciver::waitPred()
{
return interuptFlag|newsignalFlag;
}
void DummyReciver::DoJob(DummyReciver* thisDummyReciver)
{
while (true)
{
vector<string> buffer;
{
unique_lock<mutex> lck(*thisDummyReciver->queLock);
thisDummyReciver->newSignalCondition->wait(lck,bind(mem_fun(&DummyReciver::waitPred), thisDummyReciver));
if(thisDummyReciver->interuptFlag)
return;
while (!thisDummyReciver->signalQueue.empty())
{
for(int i=0;i<thisDummyReciver->signalQueue.size();++i)
{
buffer.push_back(thisDummyReciver->signalQueue.front());
thisDummyReciver->signalQueue.pop();
}
}
thisDummyReciver->newsignalFlag=false;
}
for_each(buffer.begin(),buffer.end(), [=] (vector<string>::value_type element)
{
thisDummyReciver->processSignal(element);
});
}
}
void DummyReciver::TerminateThread()
{
{
unique_lock<mutex> lck(*queLock);
interuptFlag=true;
newSignalCondition->notify_all();
}
}
void DummyReciver::ReciveSignal(const string& Signal)
{
{
unique_lock<mutex> lck(*queLock);
signalQueue.push(Signal);
newsignalFlag=true;
newSignalCondition->notify_all();
}
}