信息队列使用举例
2015-02-01 22:50
337 查看
/*thread.c*/
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <mqueue.h>
#include <string.h>
#define QUEUE_NAME "/msgQtest"
#define MAX_SIZE (255)
#define ERROR (-1)
#define OK (0)
/*线程一*/ /*receiver*/
void thread_1(void *args){
char buffer[MAX_SIZE + 1];
mqd_t *mqd;
mqd = ((mqd_t**) args)[0];
int ret;
do{
ret = mq_receive(*mqd, buffer, MAX_SIZE, NULL);
if (ret != OK){
fprintf(stderr, "%s:%d: ", __FUN__, __LINE__);
pthread_exit(0);
}
printf("receiverd msg = %d/n",(int)buffer);
sleep (400);
}while((int)buffer != 10);
pthread_exit(0);
}
/*线程二*/ /*sender*/
void thread_2(void *args)
{
char buffer[MAX_SIZE + 1];
mqd_t *mqd;
int tmpValue = 0;
int ret;
mqd = ((mqd_t**) args)[0];
while(tmpValue != 20){
sprintf(buffer,"%d",tmpValue++);
ret = mq_send(*mqd, buffer, MAX_SIZE, (int)NULL);
if (ret != OK){
fprintf(stderr, "%s:%d: ", __func__, __LINE__);
pthread_exit(0);
}
}
pthread_exit(0);
}
int main(int argc,char *argv[]){
pthread_t id_1,id_2;
struct mq_attr attr;
mqd_t mqd = (mqd_t)NULL;
int i,ret;
void *arg[1] = {&mqd};
/* initialize the queue attributes */
attr.mq_flags = 0;
attr.mq_maxmsg = 10;
attr.mq_msgsize = MAX_SIZE;
attr.mq_curmsgs = 0;
mq_unlink(QUEUE_NAME);
mq_close(mqd);
mqd = mq_open(QUEUE_NAME,O_RDWR|O_NONBLOCK|O_CREAT|O_EXCL,0644,&attr);
if ( mqd != 0 ){
fprintf(stderr, "%s:%d: ", __func__, __LINE__);
return ERROR;
}
/*创建线程一*/
ret=pthread_create(&id_1,NULL,(void *) thread_1,arg);
if( ret != 0 )
{
printf("Create pthread error!\n");
return ERROR;
}
/*创建线程二*/
ret = pthread_create(&id_2,NULL,(void *) thread_2,arg);
if( ret != 0 )
{
printf("Create pthread error!\n");
return ERROR;
}
/*等待线程结束*/
pthread_join(id_1,NULL);
pthread_join(id_2,NULL);
mq_close(mqd);
mq_unlink(QUEUE_NAME);
return OK;
}
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <mqueue.h>
#include <string.h>
#define QUEUE_NAME "/msgQtest"
#define MAX_SIZE (255)
#define ERROR (-1)
#define OK (0)
/*线程一*/ /*receiver*/
void thread_1(void *args){
char buffer[MAX_SIZE + 1];
mqd_t *mqd;
mqd = ((mqd_t**) args)[0];
int ret;
do{
ret = mq_receive(*mqd, buffer, MAX_SIZE, NULL);
if (ret != OK){
fprintf(stderr, "%s:%d: ", __FUN__, __LINE__);
pthread_exit(0);
}
printf("receiverd msg = %d/n",(int)buffer);
sleep (400);
}while((int)buffer != 10);
pthread_exit(0);
}
/*线程二*/ /*sender*/
void thread_2(void *args)
{
char buffer[MAX_SIZE + 1];
mqd_t *mqd;
int tmpValue = 0;
int ret;
mqd = ((mqd_t**) args)[0];
while(tmpValue != 20){
sprintf(buffer,"%d",tmpValue++);
ret = mq_send(*mqd, buffer, MAX_SIZE, (int)NULL);
if (ret != OK){
fprintf(stderr, "%s:%d: ", __func__, __LINE__);
pthread_exit(0);
}
}
pthread_exit(0);
}
int main(int argc,char *argv[]){
pthread_t id_1,id_2;
struct mq_attr attr;
mqd_t mqd = (mqd_t)NULL;
int i,ret;
void *arg[1] = {&mqd};
/* initialize the queue attributes */
attr.mq_flags = 0;
attr.mq_maxmsg = 10;
attr.mq_msgsize = MAX_SIZE;
attr.mq_curmsgs = 0;
mq_unlink(QUEUE_NAME);
mq_close(mqd);
mqd = mq_open(QUEUE_NAME,O_RDWR|O_NONBLOCK|O_CREAT|O_EXCL,0644,&attr);
if ( mqd != 0 ){
fprintf(stderr, "%s:%d: ", __func__, __LINE__);
return ERROR;
}
/*创建线程一*/
ret=pthread_create(&id_1,NULL,(void *) thread_1,arg);
if( ret != 0 )
{
printf("Create pthread error!\n");
return ERROR;
}
/*创建线程二*/
ret = pthread_create(&id_2,NULL,(void *) thread_2,arg);
if( ret != 0 )
{
printf("Create pthread error!\n");
return ERROR;
}
/*等待线程结束*/
pthread_join(id_1,NULL);
pthread_join(id_2,NULL);
mq_close(mqd);
mq_unlink(QUEUE_NAME);
return OK;
}
相关文章推荐
- 0905使用msgsnd函数向消息队列中发送一个字符串数据信息“Hello!This is a te
- 使用队列的程序举例(2)
- 使用storm处理消息队列中的日志信息遇见的错误
- 使用队列的程序举例(1)
- UDP高频发包,导致信息丢失,使用队列处理
- 使用WMI获取远程机器操作系统的详细信息
- 使用wwAppConfiguration类库来轻松读写应用程序配置信息
- 使用C#批量修改域帐户信息
- 使用dbms_rowid包获得rowid的详细信息
- 使用Ado.net获取数据库架构信息
- Asp.Net中检查登陆信息,使用全局过程 pcsky(原作)
- 使用Acctinfo.dll了解更多AD用户帐号信息
- 使用C#批量修改域帐户信息全部代码
- Windows中使用 tracert 命令检测目标地址路由信息
- 使用JSP读取客户端信息
- PHP中如何使用header发送头部信息
- PHP中如何使用header发送头部信息
- 使用信息提示组件 Alert
- Asp.Net中检查登陆信息(使用全局过程,原创)
- 使用C#获得系统信息