C++消息通知技巧汇总(9个实用方法) - std::string(static_cast<
在C++中,消息通知是一种常见的需求,用于在程序的不同部分之间传递信息。以下是9个实用的C++消息通知技巧:
使用全局变量 全局变量是一种可以在程序的任何地方访问的变量。通过将需要发送的消息存储在一个全局变量中,你可以在程序的任何部分调用该函数并显示消息。
#include <iostream>
int main() {
const int message = "Hello, World!";
std::cout << message << std::endl;
return 0;
}
使用函数指针 函数指针允许你将一个函数作为参数传递给另一个函数。通过将需要发送的消息作为函数指针的参数,你可以在程序的任何部分调用该函数并显示消息。
#include <iostream>
void printMessage(const char* message) {
std::cout << message << std::endl;
}
int main() {
printMessage("Hello, World!");
return 0;
}
使用回调函数 回调函数是一种允许你在特定事件发生时执行操作的机制。通过将需要发送的消息作为回调函数的参数,你可以在程序的任何部分调用该函数并显示消息。
#include <iostream>
struct MessageCallback {
void (*callback)(const char* message) = nullptr;
};
void printMessage(const char* message) {
if (callback) {
callback(message);
}
}
int main() {
MessageCallback callback = [](const char* message) {
std::cout << message << std::endl;
};
printMessage("Hello, World!");
return 0;
}
使用信号和槽 信号和槽是一种多线程编程技术,允许你在不同的线程之间传递消息。通过使用信号和槽,你可以在主线程中发送消息到其他线程,并在其他线程中处理这些消息。
#include <iostream>
#include <thread>
#include <atomic>
std::atomic<bool> messageReceived(false);
void sendMessage(const char* message) {
messageReceived = true;
std::cout << "Message received: " << message << std::endl;
}
void receiveMessage() {
while (messageReceived) {
std::cout << "Message: " << std::string(static_cast<char*>(new char[strlen(message)]), message) << std::endl;
messageReceived = false;
}
}
int main() {
std::thread t1(receiveMessage);
std::thread t2(receiveMessage);
t1.join();
t2.join();
return 0;
}
使用队列 队列是一种数据结构,可以安全地存储和检索元素。通过使用队列,你可以在程序的不同部分之间传递消息,并在需要时从队列中取出消息。
#include <iostream>
#include <queue>
#include <string>
std::queue<std::string> messages;
void sendMessage(const std::string& message) {
messages.push(message);
}
void receiveMessage() {
while (!messages.empty()) {
std::string message = messages.front();
messages.pop();
std::cout << "Message: " << message << std::endl;
}
}
int main() {
sendMessage("Hello, World!");
receiveMessage();
return 0;
}
使用条件变量 条件变量是一种同步原语,允许多个线程等待某个条件为真。通过使用条件变量,你可以在主线程中设置一个条件,并在其他线程中检查这个条件是否为真。如果条件为真,则从条件变量中获取消息。
#include <iostream>
#include <thread>
#include <condition_variable>
#include <mutex>
std::mutex mtx;
std::condition_variable cv;
bool condition = false;
void sendMessage(const std::string& message) {
std::unique_lock<std::mutex> lock(mtx);
condition = true;
cv.notify_one();
}
void receiveMessage() {
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, [&] { return condition; });
std::cout << "Message: " << message << std::endl;
}
int main() {
std::thread t1(receiveMessage);
std::thread t2(receiveMessage);
t1.join();
t2.join();
return 0;
}
使用异步消息队列 异步消息队列是一种允许在后台线程中发送和接收消息的技术。通过使用异步消息队列,你可以在主线程中发送消息到后台线程,并在后台线程中处理这些消息。这可以提高程序的性能,因为它允许主线程继续执行其他任务,而不需要等待后台线程完成消息的处理。
#include <boost/asio.hpp>
#include <iostream>
#include <thread>
#include <future>
#include <chrono>
#include <functional>
using boost::asio::ip::tcp;
using boost::asio::ip::udp;
using namespace std::chrono;
using namespace std::future_access;
using namespace boost::asio;
void sendMessage(const std::string& message) {
async_write(socket, message.c_str(), message.size());
}
void receiveMessage() {
future<void> result = async_read_until(socket, "\n", buffer);
result.get(); // Wait for the newline character to be read.
}
int main() {
boost::asio::io_service io_service;
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 12345));
for (;;) {
tcp::socket socket(io_service);
acceptor.accept(socket);
boost::asio::async_write(socket, sendMessage("Hello, World!"), receiveMessage);
}
return 0;
}
使用共享内存通信 共享内存是一种允许不同进程之间进行通信的技术。通过使用共享内存,你可以在主线程中创建一个共享内存区域,并在其他线程中写入消息。在其他线程中,你可以读取这些消息并将其显示出来。这可以作为一种简单的方式来实现跨线程的消息通知。
#include <sys/mman.h>
#include <unistd.h> 拼多多帮助力入口
#include <fcntl.h>
#include <cstring>
#include <iostream>
#include <thread>
#include <vector>
#include <mutex>
#include <condition_variable>
#include <functional>
#include <chrono>
#include <future>
#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/interprocess_condition.hpp>
#include <boost/interprocess/sync/interprocess_queue.hpp>
#include <boost/interprocess/sync/interprocess_timed_queue.hpp>
#include <boost/interprocess/sync/interprocess_timer.hpp>
#include <boost/interprocess/sync/interprocess_timer_handle.hpp>
#include <boost/interprocess/sync/interprocess_timer_event.hpp>
#include <boost/interprocess/sync/interprocess_timer_event_handler.hpp>
#include <boost/interprocess/sync/interprocess_timer_event_handler_base.hpp>
#include <boost/interprocess/sync/interprocess_timer_event_handler_base_nothrow.hpp>
#include <boost/interprocess/sync/interprocess_timer_event_handler_base_nothrow_nothrow.hpp>
#include <boost/interprocess/sync/interprocess_timer_event_handler_base_nothrow_nothrow_nothrow.hpp>
#include <boost/interprocess/sync/interprocess_timer_event_handler_base_nothrow_nothrow_nothrow_nothrow.hpp>
#include <boost/interprocess/sync/interprocess_timer_event_handler_base_nothrow_nothrow_nothrow_nothrow.hpp>
#include <boost/interprocess/sync/interprocess_timer_event_handler_base_nothrow_nothrow_nothrow_nothrow.hpp>
#include <boost/interprocess/sync/interprocess_timer_event_handler_base_nothrow_nothrow_nothrow_nothrow.hpp>
#include <boost/interprocess/sync/interprocess_timer_event_handler_base_nothrow_nothrow_nothrow_nothrow.hpp>
#include <boost/interprocess/sync/interprocess_timer_event_handler_base_nothrow_nothrow_nothrow_nothrow.hpp>
#include <boost/interprocess/sync/interprocess_timer_event_handler_base_nothrow_nothrow_nothrow_nothrow.hpp>
#include <boost/interprocess/sync/interprocess_timer_event_handler_base_nothrow_nothrow_nothrow_nothrow.hpp>
#include <boost/interprocess/sync/interprocess_timer_event_handler_base_nothrow_nothrow_nothrow_nothrow.hpp>
#include <boost/interprocess/sync/interprocess_timer_event_handler_base_nothrow_nothrow_nothrow_nothrow.hpp>
#include <boost/interprocess/sync/interprocess_timer_event_handler_base_nothrow_nothrow_nothrow_nothrow.hpp>
#include <boost/interprocess/sync/interprocess_timer_event_handler_base_nothrow_nothrow_nothrow_nothrow.hpp>
#include <boost/interprocess/sync/interprocess_timer_event_handler_base_nothrow_nothrow_nothrow_nothrow.hpp>
#include <boost/interprocess/sync/interprocess_timer_event_handler_base_nothrow_nothrow_nothrow_nothrow.hpp>
#include <boost/interprocess/sync/interprocess_timer_event_handler_base_nothrow_nothrow_nothrow_nothrow.hpp>
#include <boost/interprocess/sync/interprocess_timer_event_handler_base_nothrow_nothrow_nothrow_nothrow.hpp>
#include <boost/interprocess/sync/interprocess_timer_event_handler_base_nothrow_nothrow_nothrow_nothrow.hpp>
#include <boost/interprocess/sync/interprocess_timer_event_handler_base_nothrow_nothrow_nothrow_nothrow.hpp>
#include <boost/interprocess/sync/interprocess_timer_event_handler_base_nothrow_nothrow_nothrow_norunner.hpp>
#include <boost/interprocess/sync/interprocess_timer_event_handler_base_nothrow_nothrow_nothrow_nothrow.hpp>
#include <boost/interprocess/sync/interprocess_timer_event_handler_base_nothrow_nothrow_nothrow_nothrow.hpp>
#include <boost/interprocess/sync/interprocess_timer_event_handler_base_nothrow_nothrow_nothrow_nothrow.hpp>
#include <boost/interprocess/sync/interprocess_timer_event_handler_base_nothrow_nothrow_nothrow.hpp