-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnthread.cpp
45 lines (39 loc) · 1.39 KB
/
nthread.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
/* This class manage a thread takes the information that arrive from a queue */
#include "nthread.hpp"
class BaseListener;
/*Run as a thread*/
void NThread::Run() {
/*Loop catching requests from the module*/
while(1) {
std::cout << "im running" << std::endl;
std::unique_lock<std::mutex> ulCV(mutexCV);
/* Noone still filled the queue wait for it */
if (notificationQueue.size() == 0) cv.wait(ulCV);
else ulCV.unlock();
/*Take the element for the queue */
std::unique_lock<std::mutex> lockCond(mutexNotificationQueue);
QThread req = notificationQueue.front();
notificationQueue.pop();
lockCond.unlock();
BaseListener *listener = req.listener;
void *event = req.event;
std::lock_guard<std::mutex> lg(*req.mutexRM);
if(listener)
(*listener)(event);
}
}
void NThread::Push(BaseListener *listener, void* event, std::mutex *mutexRM) {
QThread elem;
elem.listener = listener;
elem.event = event;
elem.mutexRM = mutexRM;
std::cout << "Pushing" << std::endl;
std::unique_lock<std::mutex> notQueueLock(mutexNotificationQueue);
notificationQueue.push(elem);
notQueueLock.unlock();
/* notify the bus that the queue is not empty anymore */
std::lock_guard<std::mutex> lgCV(mutexCV);
std::cout << "notifying" << std::endl;
cv.notify_one();
std::cout << "Notified size: ;" << notificationQueue.size() << std::endl;
}