在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( ╯□╰ ))会出现乱码或者直接无法运行. 出现乱码的原因很简单:编码与译码的方式不一致!!!!!!!!!!! 首先大家需要知 ...
随机推荐
- TF-IDF与余弦相似性的应用(一):自动提取关键词 - 阮一峰的网络日志
TF-IDF与余弦相似性的应用(一):自动提取关键词 - 阮一峰的网络日志 TF-IDF与余弦相似性的应用(一):自动提取关键词 作者: 阮一峰 日期: 2013年3月15日 ...
- 6个最佳的开源Python应用服务器
6个最佳的开源Python应用服务器 首先,你知道什么是应用服务器吗?应用服务器通常被描述为是存在于服务器中心架构中间层的一个软件框架. AD: 首先,你知道什么是应用服务器吗?应用服务器通常被描述为 ...
- boost操作xml 5分钟官方教程
Five Minute Tutorial This tutorial uses XML. Note that the library is not specifically bound to XML, ...
- 最近盯着accesslog看,发现许多奇怪的东东
1.spider,各式各样的spider,就像海里的游鱼 有大的,有小的 2.各类探测http代理的spider,比如这种日志 60.173.14.85 - - [03/Sep/2013:09:59: ...
- [放松一下] 经典高清电影合集 170G BT种子下载
经典高清电影合集 170G BT种子下载 点击文件名下载 经典高清电影合集170G BT种子.torrent 下载方法 经典高清电影合集详情见目录: 1. 杀手47 2. 这个杀手不太冷 3. 放牛班 ...
- Winform - 判断GroupBox控件中的TextBox文本框是不是为空
foreach (Control item in this.groupBox2.Controls) { if (item is TextBox) { if (item.Text.Trim() == & ...
- C++ 需要返回值的函数却没有返回值的情况 单例模式
昨天在看前些天写的代码,发现一个错误. #include <iostream> using namespace std; class singleton { public: static ...
- 为 Devops 和系统管理员提供的 400+ 免费资源
014年,谷歌索引的数据量大约为200TB(1T等于1024 GB).而且,据估计,谷歌的200TB只占到整个互联网的0.004%.基本上,互联网是一个拥有无限的信息的地方. 因此,为了努力降低搜索和 ...
- Spring MVC 多选框 绑定 Entity 中的 list 属性
问题描述: 有两个类:Record.java 和 User.java,Record中有个attenders属性,是List<User>类型. 我想绑定Record中的attenders.网 ...
- 高斯消元法~get√
高斯消元法,是线性代数中的一个算法,可用来求解线性方程组,并可以求出矩阵的秩,以及求出可逆方阵的逆矩阵.高斯消元法的原理是:若用初等行变换将增广矩阵 化为 ,则AX = B与CX = D是同解方程组. ...