【发布时间】:2021-05-26 01:13:09
【问题描述】:
编辑 还尝试了 boost 1.75
我正在学习如何使用 boost::interprocess 消息队列,
我正在使用文档here 中的示例
有一个不同的 - 对于我正在使用的另一个进程fork()
但我得到了
boost::interprocess_exception::library_error
当我试图从队列中读取数据时; 我在 Centos 7.6 上运行 boost 1.58
这是我的代码:
#include <iostream>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/ipc/message_queue.hpp>
#include <vector>
[[no_discard]]bool RunChiled()
{
using namespace boost::interprocess;
try {
message_queue mq(open_or_create //open or create
, "message_queue" //name
, 100 //max message number
, 100 //max message size
);
message_queue::size_type recvd_size;
unsigned int priority = 0;
for (auto num = 0; num < 100; ++num) {
int number = 0;
mq.try_receive(&number, sizeof(int), recvd_size, priority);
if (number != num || recvd_size != sizeof(number))
return 1;
}
}catch(interprocess_exception &ex) {
message_queue::remove("message_queue");
std::cout << ex.what() << std::endl;
return 1;
}
message_queue::remove("message_queue");
return true;
}
int main() {
using namespace boost::interprocess;
message_queue::remove("message_queue");
auto pid = fork();
if(pid > 0)
{
sleep(2);
auto res = RunChiled();
std::cout << res;
} else if(pid == 0)
{
boost::interprocess::message_queue mq(create_only,"message_queue", 100, 100);
for(int i=0; i<100; ++i)
mq.send(&i,sizeof(i),0);
}
return 0;
}
【问题讨论】:
-
你没有说你在什么平台下运行,但这个线程可能会有所帮助:stackoverflow.com/a/49198130/421195 PS:“RunChiled()”?还是你的意思是
"RunChild()"? -
@paulsm4 我在 Centos 7.6 上运行 boost 1.58
标签: c++ boost c++17 boost-interprocess