我想要的是使用名为ShmObj的类访问托管共享内存对象的数据信息,其中指向共享对象的原始指针作为私有成员,如下面的代码块.
我的问题是主程序分段错误.我猜绝对原始指针会导致问题.我试图将原始指针更改为bi :: offset_ptr,但没有帮助.任何帮助表示赞赏.
ShmObj.h
#include <string>
#include <boost/interprocess/managed_shared_memory.hpp>
namespace bi = boost::interprocess;
class ShmObj {
public:
ShmObj() {
bi::managed_shared_memory segment(bi::open_only, "shm");
pNum = segment.find<int>("Number").first;
}
int getNumber() {return *pNum;}
virtual ~ShmObj() {}
private:
int* pNum;
};
Run Code Online (Sandbox Code Playgroud)
main.cpp中
#include "ShmObj.h"
#include <iostream>
int main() {
ShmObj X;
std::cout << X.getNumber() << std::endl;
}
Run Code Online (Sandbox Code Playgroud) 我想要的是当一个消息队列接收到一个 int N 时,处理函数将在 N 秒后被调用。下面是我的代码。
如果两个近消息队列的持续时间秒数大于 int N,则运行正常,但是当两个接收到的消息队列之间的持续时间秒数小于 N 时,处理程序将在一个处理程序中打印“操作已取消”,这不是我想要的想。
如果您有任何帮助,我将不胜感激。
#include <boost/asio.hpp>
#include <zmq.h>
#include <boost/thread.hpp>
#include <iostream>
boost::asio::io_service io_service;
void* context = zmq_ctx_new();
void* sock_pull = zmq_socket(context, ZMQ_PULL);
void handler(const boost::system::error_code &ec) {
std::cout << "hello, world" << "\t" << ec.message() << std::endl;
}
void run() {
io_service.run();
}
void thread_listener() {
int nRecv;
boost::asio::deadline_timer timer(io_service, boost::posix_time::seconds(0));
while( true ) {
zmq_recv(sock_pull, &nRecv, sizeof(nRecv), 0);
std::cout << nRecv << std::endl;
timer.expires_from_now(boost::posix_time::seconds(nRecv));
timer.async_wait(handler);
}
}
int main(int argc, …Run Code Online (Sandbox Code Playgroud)