C++ POSIX消息队列,发送和接收
我有多个线程使用POSIX消息队列进行通信C++ POSIX消息队列,发送和接收,c++,posix,message-queue,C++,Posix,Message Queue,我有多个线程使用POSIX消息队列进行通信 double Pi_Msg(unsigned int p, unsigned int nn) { double *pis = new double[p]; struct mq_attr attr; mqd_t mqdes; char buf[256]; unsigned int prio; attr.mq_maxmsg = p; attr.mq_msgsize = 256; attr.mq
double Pi_Msg(unsigned int p, unsigned int nn)
{
double *pis = new double[p];
struct mq_attr attr;
mqd_t mqdes;
char buf[256];
unsigned int prio;
attr.mq_maxmsg = p;
attr.mq_msgsize = 256;
attr.mq_flags = 0;
mqdes = mq_open("mq", O_RDWR|O_CREAT, 0664, &attr);
for (unsigned int i = 0; i < p; i++) {
int pid = fork();
if (pid < 0) {
exit(1);
} else if (pid != 0) {
wait();
mq_receive(mqdes, &buf[0], 256, NULL);
pis[i] = atoi(buf);
} else {
double pi = 3.1415;
char str[128];
sprintf(str, "%f", pi);
mq_send(mqdes, str, 128, 0);
exit(EXIT_SUCCESS);
}
}
}
double Pi_Msg(无符号整数p,无符号整数nn)
{
double*pis=新的double[p];
结构mq_attr attr;
mqd_t mqdes;
char-buf[256];
无符号整数优先;
attr.mq_maxmsg=p;
attr.mq_msgsize=256;
attr.mq_标志=0;
mqdes=mq_open(“mq”,O_RDWR | O_CREAT,0664和attr);
for(无符号整数i=0;i
ms_receive始终返回-1,我不知道为什么。在接收失败后打印出
errno
了解更多信息。您的示例不完整-请尝试发布一个完整的示例,其他人可以编译和运行。