#include <iostream>
#include <vector>
#include <queue>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <cstring>
#include <chrono>
// 自定义消息结构
template <typename CallbackType>
struct CustomMessage {
CallbackType callback;
void* data;
size_t length;
CustomMessage(CallbackType callback, void* data, size_t length)
: callback(callback), data(data), length(length) {}
CustomMessage() = default; // 添加默认构造函数
};
// 消息队列
template <typename CallbackType>
class MessageQueue {
public:
void push(const CustomMessage<CallbackType>& message) {
std::unique_lock<std::mutex> lock(mutex);
while (queue.size() >= maxQueueSize) {
// 队列已满,等待
notFull.wait(lock);
}
queue.push(message);
lock.unlock();
notEmpty.notify_one(); // 通知观察者队列不为空
}
bool pop(CustomMessage<CallbackType>& message) {
std::unique_lock<std::mutex> lock(mutex);
while (queue.empty() && !finished) {
// 队列为空且未完成,等待
notEmpty.wait(lock);
}
if (!queue.empty()) {
message = queue.front();
queue.pop();
lock.unlock();
notFull.notify_one(); // 通知发布者队列不满
return true;
}
return false;
}
void setFinished() {
std::unique_lock<std::mutex> lock(mutex);
finished = true;
lock.unlock();
notEmpty.notify_all(); // 唤醒等待的观察者线程
}
private:
std::queue<CustomMessage<CallbackType>> queue;
const size_t maxQueueSize = 5; // 队列最大容量
bool finished = false;
std::mutex mutex;
std::condition_variable notEmpty;
std::condition_variable notFull;
};
// 抽象观察者
template <typename CallbackType>
class Observer {
public:
virtual void update(const CustomMessage<CallbackType>& message) = 0;
};
// 具体观察者
template <typename CallbackType>
class ConcreteObserver : public Observer<CallbackType> {
public:
ConcreteObserver(const std::string& name, MessageQueue<CallbackType>& messageQueue)
: name(name), messageQueue(messageQueue) {}
void update(const CustomMessage<CallbackType>& message) override {
std::this_thread::sleep_for(std::chrono::seconds(3));
std::cout << name << " 收到消息,长度: " << message.length << std::endl;
// 处理消息
message.callback(message.data, message.length);
std::this_thread::sleep_for(std::chrono::seconds(3));
}
void start() {
while (true) {
CustomMessage<CallbackType> message;
if (messageQueue.pop(message)) {
update(message);
} else {
break; // 队列已完成
}
}
}
private:
std::string name;
MessageQueue<CallbackType>& messageQueue;
};
// 具体发布者
template <typename CallbackType>
class Publisher {
public:
Publisher(MessageQueue<CallbackType>& messageQueue) : messageQueue(messageQueue) {}
void publishMessages(const std::vector<CustomMessage<CallbackType>>& messages) {
for (const CustomMessage<CallbackType>& message : messages) {
messageQueue.push(message);
}
messageQueue.setFinished();
}
private:
MessageQueue<CallbackType>& messageQueue;
};
int main() {
MessageQueue<std::function<void(void*, size_t)>> messageQueue;
Publisher<std::function<void(void*, size_t)>> publisher(messageQueue);
ConcreteObserver<std::function<void(void*, size_t)>> observer1("观察者1", messageQueue);
ConcreteObserver<std::function<void(void*, size_t)>> observer2("观察者2", messageQueue);
// 示例回调函数
auto callback = [](void* data, size_t length) {
char* messageData = static_cast<char*>(data);
std::cout << "回调函数处理消息数据: " << std::string(messageData, length) << std::endl;
};
std::vector<CustomMessage<std::function<void(void*, size_t)>>> messages;
char messageData[] = "自定义消息";
CustomMessage<std::function<void(void*, size_t)>> customMessage(callback, messageData, strlen(messageData));
messages.push_back(customMessage);
messages.push_back(customMessage);
messages.push_back(customMessage);
messages.push_back(customMessage);
messages.push_back(customMessage);
messages.push_back(customMessage);
messages.push_back(customMessage);
std::thread([&]() {
publisher.publishMessages(messages);
}).detach();
std::thread observerThread1(&ConcreteObserver<std::function<void(void*, size_t)>>::start, &observer1);
std::thread observerThread2(&ConcreteObserver<std::function<void(void*, size_t)>>::start, &observer2);
observerThread1.join();
observerThread2.join();
return 0;
}
#include <iostream>
#include <vector>
#include <queue>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <cstring>
#include <chrono>
#include <algorithm>
// 自定义消息结构
template <typename CallbackType>
struct CustomMessage {
CallbackType callback;
void* data;
size_t length;
CustomMessage(CallbackType callback, void* data, size_t length)
: callback(callback), data(data), length(length) {}
CustomMessage() = default;
};
// 消息队列
template <typename CallbackType>
class MessageQueue {
public:
void push(const CustomMessage<CallbackType>& message) {
std::unique_lock<std::mutex> lock(mutex);
while (queue.size() >= maxQueueSize) {
notFull.wait(lock);
}
queue.push(message);
lock.unlock();
notEmpty.notify_one();
}
bool pop(CustomMessage<CallbackType>& message) {
std::unique_lock<std::mutex> lock(mutex);
while (queue.empty() && !finished) {
notEmpty.wait(lock);
}
if (!queue.empty()) {
message = queue.front();
queue.pop();
lock.unlock();
notFull.notify_one();
return true;
}
return false;
}
void setFinished() {
std::unique_lock<std::mutex> lock(mutex);
finished = true;
lock.unlock();
notEmpty.notify_all();
}
private:
std::queue<CustomMessage<CallbackType>> queue;
const size_t maxQueueSize = 5;
bool finished = false;
std::mutex mutex;
std::condition_variable notEmpty;
std::condition_variable notFull;
};
// 抽象观察者
template <typename CallbackType>
class Observer {
public:
virtual void update(const CustomMessage<CallbackType>& message) = 0;
};
// 具体观察者
template <typename CallbackType>
class ConcreteObserver : public Observer<CallbackType> {
public:
ConcreteObserver(const std::string& name)
: name(name) {}
void update(const CustomMessage<CallbackType>& message) override {
std::cout << name << " 收到消息,长度: " << message.length << std::endl;
message.callback(message.data, message.length);
}
private:
std::string name;
};
// 具体发布者
template <typename CallbackType>
class Publisher {
public:
Publisher(MessageQueue<CallbackType>& messageQueue) : messageQueue(messageQueue) {}
void publishMessages(const std::vector<CustomMessage<CallbackType>>& messages) {
for (const CustomMessage<CallbackType>& message : messages) {
messageQueue.push(message);
}
messageQueue.setFinished();
}
private:
MessageQueue<CallbackType>& messageQueue;
};
// 具体主题
template <typename CallbackType>
class ConcreteSubject {
public:
void addObserver(Observer<CallbackType>* observer) {
observers.push_back(observer);
}
void removeObserver(Observer<CallbackType>* observer) {
auto it = std::find(observers.begin(), observers.end(), observer);
if (it != observers.end()) {
observers.erase(it);
}
}
void notifyObservers() {
while (true) {
CustomMessage<CallbackType> message;
if (messageQueue.pop(message)) {
for (Observer<CallbackType>* observer : observers) {
observer->update(message);
}
} else {
break;
}
}
}
void startObservers() {
for (Observer<CallbackType>* observer : observers) {
observerThreads.push_back(std::thread(&ConcreteSubject::runObserver, this, observer));
}
}
void joinObservers() {
for (std::thread& thread : observerThreads) {
thread.join();
}
}
void runObserver(Observer<CallbackType>* observer) {
// 观察者线程等待消息
CustomMessage<CallbackType> message;
while (messageQueue.pop(message)) {
observer->update(message);
}
}
MessageQueue<CallbackType>& getMessageQueue() { return messageQueue; }
private:
std::vector<Observer<CallbackType>*> observers;
MessageQueue<CallbackType> messageQueue;
std::vector<std::thread> observerThreads;
};
int main() {
ConcreteSubject<std::function<void(void*, size_t)>> subject;
ConcreteObserver<std::function<void(void*, size_t)>> observer1("观察者1");
ConcreteObserver<std::function<void(void*, size_t)>> observer2("观察者2");
subject.addObserver(&observer1);
subject.addObserver(&observer2);
char messageData[] = "自定义消息"; // ??????????
std::thread([&]() {
auto callback = [](void* data, size_t length) {
char* messageData = static_cast<char*>(data);
std::cout << "回调函数处理消息数据: " << std::string(messageData, length) << std::endl;
};
CustomMessage<std::function<void(void*, size_t)>> customMessage(callback, messageData, strlen(messageData));
std::vector<CustomMessage<std::function<void(void*, size_t)>>> messages;
messages.push_back(customMessage);
messages.push_back(customMessage);
messages.push_back(customMessage);
messages.push_back(customMessage);
messages.push_back(customMessage);
messages.push_back(customMessage);
Publisher<std::function<void(void*, size_t)>> publisher(subject.getMessageQueue());
publisher.publishMessages(messages);
}).detach();
subject.startObservers();
subject.joinObservers();
return 0;
}