linux 中msgque的使用demo
server端,默认为阻塞模式,msgsnd 超过队列的的缓冲后会报错误。直到队列可存放数据。
/*******************************************************************************
* function name : msgserver_handle
* description : heartbeat function ,if receive new data ,clear counter,or,
* call heatbeat main func
* param[in] : task_table[4]
* param[out] : none
* return : none
*******************************************************************************/
int *msgserver_handle (void *arg)
{
int id = 0;
static char pthread_flg = 0;
if(pthread_flg ==1)
return 0;
pthread_flg = 1;
key_t key = ftok("/tmp",66);
id = msgget(key,IPC_CREAT | 0666);
if(id == -1)
{
printf("create msg error \n");
return 0;
}
while(videoIsRun==1)
{
ckxmsg.mtype = 1;
if(running ==1){
LOG1("send msg %s %d\n", ckxmsg.mtext,ckxmsg.time_difference);
if(msgsnd(id,(void *)&ckxmsg,512,0) < 0)
{
printf("send msg error \n");
return 0;
}
memset((void *)&ckxmsg,0,sizeof(ckxmsg));
running =0;
}
usleep(10000);
if(strncmp(msg,"QUIT",4) == 0)
break;
}
if(msgctl(id,IPC_RMID,NULL) < 0)
{
printf("del msg error \n");
return 0;
}
return 0;
}
客户端 使用非阻塞模式实时读取,没有数据时返回-1,receive msg error,在qt中使用
void Mainframe::ReadsharedMemoryFromcamera()
{
static char accessflg =0;
static unsigned int readcnt =0;
if(accessflg == 0){
accessflg = 1 ;
key_t key = ftok("/tmp",66);
id = msgget(key,0666|IPC_CREAT);
if(id == -1)
{
qDebug()<<"open msg error \n";
}
qDebug()<<"open msgget id " <<id ;
label_detec_value->setText("Detecting");
}
readcnt ++;
if(readcnt >= 10){
readcnt = 10;
memset(ckxmsg.mtext,0,512);
if(msgrcv(id,(void *)&ckxmsg,512,1,IPC_NOWAIT ) < 0)//no wait
{
qDebug()<<"receive msg error \n";
}else {
qDebug() << "receive data is "<< ckxmsg.mtext ;
if(0 == memcmp(ckxmsg.mtext,"exsist",6)){
QString str_e(ckxmsg.mtext);
label_detec_value->setText(str_e);
}
else {
label_detec_value->setText("Detecting");
}
}
}
}


