在Windows系统上实现轻量级的线程间及进程间消息队列
Windows没有message queue累世的IPC内核对象,使得在在处理IPC时少了一种传递消息的手段。
利用Windows的Naming Object可以实现一套简单的Inter-Thread消息队列。这里并不使用socket,因为一旦使用socket,就得负责port管理,很麻烦,另外在对外接口上也很难和vxworks等msgq接口保持一致。所以后来干脆把接口定义成了类vxworks接口。
偶然间看了一眼云风的http://blog.codingnow.com/中关于windows进程间内存共享的blog,决定将其改成同时支持Inter-Thread和Inter-Process Message Queue的有名对象。
目前接口定义如下, 可下载包见 tinymq-binary-0.1.zip 以及测试 demo-0.1.zip
/* msgQueue.h - declaration of VxWorks-like message queue */
/*
* This file has no copyright assigned and is placed in the Public Domain.
* This file is a part of the virtual operating system package.
* No warranty is given; refer to the file DISCLAIMER within the package.
*
*/
/*
modification history
--------------------
01a,11nov11,sgu created
*/
/*
DESCRIPTION
This module implements the functions for a message queue. The message queue
manipulation functions in this file are similar with the Wind River VxWorks
kernel message queue interfaces.
The memory architecture for a message queue:
---------------- -----------------------------------------------------------
| local memory |-->| shared memory |
---------------- -----------------------------------------------------------
^ ^
| |
---------------- -----------------------------------------------------------
| MSG_Q | | MSG_SM | MSG_NODE list | message queue data |
---------------- -----------------------------------------------------------
^ ^
| |
--------------------------------------------- |
| MSG_NODE1 | MSG_NODE2 | ... | MSG_NODE(N) | |
--------------------------------------------- |
|
---------------------------------
| data1 | data2 | ... | data(N) |
---------------------------------
Each message queue in memory can be divided into two parts, local memory and
shared memory, but these two parts are not closed by. The local memory can be
accessed in an process, also can be accessed between threads in the process.
The shared memory can be accessed between threads in a process if the message
queue name is NULL; or can be accessed between processes if the message queue
name is not NULL. There is one data structure MSG_Q in local memory; three data
structures -- MSG_SM, MSG_NODE list and message queue data in shared memory.
The structure MSG_Q saves the kernel objects handlers and the shared memory
address; MSG_SM saves the message queue attributes; MSG_NODE list saves all the
nodes for the message queue, and each node saves all attribute of each message;
the message queue data area saves all the data for all the message. All the
structures defined below.
If you meet some problem with this module, please feel free to contact
me via e-mail: gushengyuan2002@163.com
*/
#ifndef _MSG_QUEUE_H_
#define _MSG_QUEUE_H_
/* defines */
/* wait forever for timeout flag */
#define WAIT_FOREVER -1
/* version string length */
#define VERSION_LEN 8
/* create an inter-thread message queue */
#define msgQCreate(maxMsgs, maxMsgLength, options) \
msgQCreateEx(maxMsgs, maxMsgLength, options, NULL)
/* typedefs */
typedef unsigned int UINT;
typedef void* MSG_Q_ID; /* message queue identify */
/* message queue options for task waiting for a message */
enum MSG_Q_OPTION{
MSG_Q_FIFO = 0x0000,
MSG_Q_PRIORITY = 0x0001
};
/* message sending options for sending a message */
enum MSG_Q_PRIORITY{
MSG_PRI_NORMAL = 0x0000, /* put the message at the end of the queue */
MSG_PRI_URGENT = 0x0001 /* put the message at the frond of the queue */
};
/* message queue status */
typedef struct tagMSG_Q_STAT {
char version[VERSION_LEN]; /* library version */
int maxMsgs; /* max messages that can be queued */
UINT maxMsgLength; /* max bytes in a message */
int options; /* message queue options */
int msgNum; /* message number in the queue */
int sendTimes; /* number of sent */
int recvTimes; /* number of received */
}MSG_Q_STAT;
#ifdef __cplusplus
extern "C"
{
#endif
/* declarations */
/*******************************************************************************
* msgQCreateEx - create a message queue, queue pended tasks in FIFO order
*
* create a message queue, queue pended tasks in FIFO order.
* <name> message name, if name equals NULL, create an inter-thread message
* queue, or create an inter-process message queue.
*
* RETURNS: MSG_Q_ID when success or NULL otherwise.
*/
MSG_Q_ID msgQCreateEx
(
int maxMsgs, /* max messages that can be queued */
int maxMsgLength,/* max bytes in a message */
int options, /* message queue options, ignored on Windows platform */
const char *name /* message name */
);
/*******************************************************************************
* msgQOpen - open a message queue
*
* open a message queue.
*
* RETURNS: MSG_Q_ID when success or NULL otherwise.
*/
MSG_Q_ID msgQOpen
(
const char * name /* message name */
);
/*******************************************************************************
* msgQDelete - delete a message queue
*
* delete a message queue.
*
* RETURNS: 0 when success or -1 otherwise.
*/
int msgQDelete
(
MSG_Q_ID msgQId /* message queue to delete */
);
/*******************************************************************************
* msgQReceive - receive a message from a message queue
*
* receive a message from a message queue.
*
* RETURNS: 0 when success or -1 otherwise.
*/
int msgQReceive
(
MSG_Q_ID msgQId, /* message queue from which to receive */
char * buffer, /* buffer to receive message */
UINT maxNBytes, /* length of buffer */
int timeout /* ticks to wait */
);
/*******************************************************************************
* msgQSend - send a message to a message queue
*
* send a message to a message queue.
*
* RETURNS: 0 when success or -1 otherwise.
*/
int msgQSend
(
MSG_Q_ID msgQId, /* message queue on which to send */
char * buffer, /* message to send */
UINT nBytes, /* length of message */
int timeout, /* ticks to wait */
int priority /* MSG_PRI_NORMAL or MSG_PRI_URGENT */
);
/*******************************************************************************
* msgQStat - get the status of message queue
*
* get the detail status of a message queue.
*
* RETURNS: 0 when success or -1 otherwise.
*/
int msgQStat
(
MSG_Q_ID msgQId,
MSG_Q_STAT * msgQStatus
);
/*******************************************************************************
* msgQShow - show the status of message queue
*
* show the detail status of a message queue.
*
* RETURNS: 0 when success or -1 otherwise.
*/
int msgQShow
(
MSG_Q_ID msgQId
);
#ifdef __cplusplus
}
#endif
#endif
在Windows系统上实现轻量级的线程间及进程间消息队列的更多相关文章
- Windows系统上搭建Clickhouse开发环境
Windows系统上搭建Clickhouse开发环境 总体思路 微软的开发IDE是很棒的,有两种:Visual Studio 和 VS Code,一个重量级,一个轻量级.近年来VS Code越来越受欢 ...
- 手把手教你玩转 Gitea|在 Windows 系统上安装 Gitea
Gitea 支持在 Windows 系统上安装和使用.Gitea 本身作为一个单体应用程序,即点即用,如需长期驻留作为后台服务并开机运行就要依靠 Windows 服务工具 sc.exe. 通过本文,你 ...
- windows系统上安装与使用Android NDK r5 (转)
windows系统上安装与使用Android NDK r5 很早就听说了android的NDK应用,只是一直没有时间去研究,今天花了点时间在windows平台搭建了NDK环境,并成功运行了第一个简单 ...
- spm完成dmp在windows系统上导入详细过程
--查询dmp字符集 cat spmprd_20151030.dmp ','xxxx')) from dual; spm完成dmp在windows系统上导入详细过程 create tablespace ...
- 快速获取Windows系统上的国家和地区信息
Windows系统上包含了200多个国家和地区的数据,有时候编程需要这些资料.以下代码可以帮助你快速获取这些信息.将Console语句注释掉,可以更快的完成分析. static void Main(s ...
- Windows系统上如何使用SSH
Windows系统上如何使用SSH 传统的网络服务程序如FTP.Telnet等,在网络上一般使用明文传送数据.用户账号和口令信息,容易受到中间人的攻击.用户利用SSH协议后能有效防止DNS及IP欺骗, ...
- 如何在Windows系统上用抓包软件Wireshark截获iPhone等网络通讯数据
http://www.jb51.net/os/windows/189090.html 今天给大家介绍一种如何在Windows操作系统上使用著名的抓包工具软件Wireshark来截获iPhone.iPa ...
- Redis进阶实践之三如何在Windows系统上安装安装Redis
一.Redis的简介 Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合 ...
- 非Unicode编码的软件如何在Windows系统上运行
我们常常会遇到这样一种情况:点开某些日文软件(我不会说就是galgame( ╯□╰ ))会出现乱码或者直接无法运行. 出现乱码的原因很简单:编码与译码的方式不一致!!!!!!!!!!! 首先大家需要知 ...
随机推荐
- spoj 1812 lcsII (后缀自动机)
spoj 1812 lcsII (后缀自动机) 题意:求多个串的lcs,最多10个串,每个串最长10w 解题思路:后缀自动机.先建好第一个串的sam,然后后面的串拿上去跑(这个过程同前一题).sam上 ...
- 集群安装配置Hadoop具体图解
集群安装配置Hadoop 集群节点:node4.node5.node6.node7.node8. 详细架构: node4 Namenode,secondnamenode,jobtracker node ...
- Notepad++中如何设置自动换行以及行宽
view-->word wrap; setting->preference-->vertical edge settings; Notepad++中如何设置自动换行以及行宽 http ...
- jquery Deferred使用经验
这周做了个小活动(http://aoqi.100bt.com/zt-2016duanzi/index.html),刚开始时候没看好需求,逻辑都写一块了 最后各种坑要填补,从中也获取了些经验和教训,下面 ...
- linux中段错误的处理
在 Linux环境下做C语言项目,由于是在一个原有项目基础之上进行二次开发,而且项目工程庞大复杂,出现了不少问题,其中遇到最多.花费时间最长的问题就是著名的“段错误”(Segmentation Fau ...
- JSP的学习(2)——语法知识一
上一篇<JSP的学习>讲述了JSP的一些基础知识和底层原理,本篇将来学习JSP所需掌握的语法知识等. JSP的语法主要包括以下几个部分的内容: 1) JSP模板元素 2) ...
- Eclipse无提示的解决办法 和 内容辅助技巧
Eclipse无提示的解决办法 和 内容辅助技巧 一.若发现内容辅助失效没有提示 下面是解决办法,现贴出来与大家共享: 1.菜单window->Preferences->Jav ...
- Perl入门(四)Perl的正則表達式
正則表達式是Perl语言的特色.主要的语法不是非常难,可是编写一个符合需求.高效的正則表達式.还是有一些挑战的. Perl的三种匹配模式 1.查找 语法:m/正則表達式内容/; 作用:查找匹配内容中是 ...
- No matching code signing identity found
真机调试过程中弹出这个问题,网上找到的解决的方法,记录一下. .... 弄完这些步骤之后,上面多出一个 IOS disturbution.所以出现这个问题的解决办法应该是设置的证书没有刷新到本地所致.
- hdu1869 六度分离(Floyd)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1869 转载请注明出处:http://blog.csdn.net/u012860063?viewmode ...