关于boost 消息队列的问题
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个结构体消息,重复同样的步骤,第二个进程第二次无法读出消息,不知道什么原因。
...全文


网络编程
