在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( ╯□╰ ))会出现乱码或者直接无法运行. 出现乱码的原因很简单:编码与译码的方式不一致!!!!!!!!!!! 首先大家需要知 ...
随机推荐
- c++11 新特性之 autokeyword
C++11是对眼下C++语言的扩展和修正.C++11包含大量的新特性:包含lambda表达式,类型推导keywordauto.decltype,和模板的大量改进. g++编译c++11命令加上 -st ...
- android绑定Service失败原因
今天抄一个代码,学习Service,中间Service的绑定一直是失败的. bindService返回false 上网查询的话都是一些,比如说TabHost的问题 发现和自己的问题不一样. 最后想了想 ...
- 13-(1-4)进程管道关于popen(-r-w)及pipe的程序使用实例
#include<unistd.h> #include<stdlib.h> #include<stdio.h> #include<string.h> # ...
- ARM过程调用标准---APCS简单介绍
介绍 APCS,ARM 过程调用标准(ARM Procedure Call Standard),提供了紧凑的编写例程的一种机制,定义的例程能够与其它例程交织在一起.最显著的一点是对这些例程来自哪里没有 ...
- 学IT技术 轻松高薪就业
如今的社会是大鱼吃小鱼的时代,假设你没有过强的技术,是非常难在社会上立足,更不要谈占有一席之地了.假设你想学一门好技术,那你想知道如今学什么技术好吗?我想这要看如今市场须要什么人才,缺什么人才.同一时 ...
- oncreate 测量尺寸
在android中,在oncreate里面只是将布局信息设置好,并没有进行布局,因此是没法进行测量view或者屏幕的长高,可以通过下面的observer来观察,当view布局完成之后会回调下面的两个接 ...
- 免插件打造wordpress投稿页面
一.新建投稿页面模板 把主题的 page.php 另存为 tougao.php,并且在第一行的 <?php 之后添加模板的标识注释: /* Template Name: tougao */ 紧接 ...
- shadow dom
初识shadow dom 我们先看个input="range"的表现: what amazing ! 一个dom能表现出这么多样式嘛? 无论是初学者和老鸟都是不肯相信的,于是在好奇 ...
- 程序员必须知道的几个Git代码托管平台(转)
上一篇博客中2015继续任性——不会Git命令,照样玩转Git我们简单的介绍了在VS2013中使用Git,和GitHub客户端的使用.那么使用Git到底有什么好处呢?最为明显的是支持Git代码托管的平 ...
- wifi定位原理
wifi定位和手机基站定位类别似,两者都需要收集wifi位置信息接入点. 其实WIFI奇妙,它靠的是侦測附近周围全部的无线网路基地台 (WiFi Access Point) 的 MAC Address ...