-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChannel.h
More file actions
38 lines (30 loc) · 938 Bytes
/
Channel.h
File metadata and controls
38 lines (30 loc) · 938 Bytes
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
#ifndef CHANNEL_H
#define CHANNEL_H
#include <queue>
#include <mutex>
#include <condition_variable>
#include <string>
#include <iostream>
#include "ThreadSafeBlockingQueue.h"
template<typename T>
class Channel {
private:
std::string name;
std::unique_ptr<ThreadSafeQueueInterface<T>> queue;
public:
// Constructors must now initialize the queue pointer with an instance of a class that implements ThreadSafeQueueInterface
Channel() : name("Unnamed"), queue(std::make_unique<ThreadSafeBlockingQueue<T>>()) {}
Channel(const std::string &name) : name(name), queue(std::make_unique<ThreadSafeBlockingQueue<T>>()) {}
void send(const T &data) {
queue->push(data);
std::cout << "Sent data to channel: " << data << std::endl;
}
T receive() {
T data = queue->waitAndPop();
return data;
}
std::string getName() const {
return name;
}
};
#endif // CHANNEL_H