关于boost 消息队列的问题

bj-alex 2014-12-08 09:03:02
	message_queue::remove("mqtest");
message_queue LEmq(create_only, "mqtest", 10, sizeof(int));
while (true)
{
try{

//Create a message_queue.
message_queue ReceiveSLEmq
(open_only //only create
, "mqtest" //name
);

//Send 10 numbers
for (int i = 0; i < 10; ++i){
LEmq.send(&i, sizeof(int), 0);
}
}
catch (interprocess_exception &ex){
std::cout << ex.what() << std::endl;
return 1;
}
}

	while (true)
{
try{
message_queue mq(open_only, // only open
"mqtest" // name
);
size_t recvd_size;
unsigned int priority;

// now send the messages to the queue
for (int i = 0; i < 10; ++i)
{
int buffer;
bool flag=mq.try_receive(&buffer, sizeof(int), recvd_size, priority);
if (recvd_size != sizeof(int))
return 0;// do the error handling
if (flag == false)
{
std::cout << "empty queue" << std::endl;
break;
}
std::cout << "NO. " << buffer << " recvd size: " << recvd_size << " priority: " << priority << std::endl;
}
}
catch (interprocess_exception& e)
{
std::cout << e.what() << std::endl;
}
}


以上两段代码为测试boost消息队列,第一段写入消息队列,第二段读取消息队列。当我将10个整数都写入队列,并由第二段代码读出,暂停第二段程序,然后再执行一遍第一段代码(删除该队列,创建队列,写入10个数字),继续执行第二段代码,仍然可以读出队列中的数字。
即:第二个进程在读取完队列后,第一个进程即使删除原队列,并重新创建同名的队列,第二个进程仍然可以正常读出。但是,当我将这里的数字,换成结构体,即存入10个结构体消息,重复同样的步骤,第二个进程第二次无法读出消息,不知道什么原因。
...全文
227 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
oyljerry 2014-12-18
  • 打赏
  • 举报
回复
读取前先查看一下队列是否真的清除了。多加一些调试信息

18,355

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧