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系统上实现轻量级的线程间及进程间消息队列的更多相关文章

  1. Windows系统上搭建Clickhouse开发环境

    Windows系统上搭建Clickhouse开发环境 总体思路 微软的开发IDE是很棒的,有两种:Visual Studio 和 VS Code,一个重量级,一个轻量级.近年来VS Code越来越受欢 ...

  2. 手把手教你玩转 Gitea|在 Windows 系统上安装 Gitea

    Gitea 支持在 Windows 系统上安装和使用.Gitea 本身作为一个单体应用程序,即点即用,如需长期驻留作为后台服务并开机运行就要依靠 Windows 服务工具 sc.exe. 通过本文,你 ...

  3. windows系统上安装与使用Android NDK r5 (转)

    windows系统上安装与使用Android NDK r5  很早就听说了android的NDK应用,只是一直没有时间去研究,今天花了点时间在windows平台搭建了NDK环境,并成功运行了第一个简单 ...

  4. spm完成dmp在windows系统上导入详细过程

    --查询dmp字符集 cat spmprd_20151030.dmp ','xxxx')) from dual; spm完成dmp在windows系统上导入详细过程 create tablespace ...

  5. 快速获取Windows系统上的国家和地区信息

    Windows系统上包含了200多个国家和地区的数据,有时候编程需要这些资料.以下代码可以帮助你快速获取这些信息.将Console语句注释掉,可以更快的完成分析. static void Main(s ...

  6. Windows系统上如何使用SSH

    Windows系统上如何使用SSH 传统的网络服务程序如FTP.Telnet等,在网络上一般使用明文传送数据.用户账号和口令信息,容易受到中间人的攻击.用户利用SSH协议后能有效防止DNS及IP欺骗, ...

  7. 如何在Windows系统上用抓包软件Wireshark截获iPhone等网络通讯数据

    http://www.jb51.net/os/windows/189090.html 今天给大家介绍一种如何在Windows操作系统上使用著名的抓包工具软件Wireshark来截获iPhone.iPa ...

  8. Redis进阶实践之三如何在Windows系统上安装安装Redis

    一.Redis的简介        Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合 ...

  9. 非Unicode编码的软件如何在Windows系统上运行

    我们常常会遇到这样一种情况:点开某些日文软件(我不会说就是galgame( ╯□╰ ))会出现乱码或者直接无法运行. 出现乱码的原因很简单:编码与译码的方式不一致!!!!!!!!!!! 首先大家需要知 ...

随机推荐

  1. Android中通过耳机按键控制音乐播放的实现

    今天在研究Android中实现Android 4.2.2源码中的Music应用的源码,关于通过耳机按键控制音乐播放的实现,有点好奇,就仔细分析了一下源码, 主要由 MediaButtonIntentR ...

  2. Spring MVC 遇到的一点点问题(转)

    今天下午下班之前看了看凯歌给的Spring Training的教程的lab篇,我之前有跟着做没有遇到什么问题,但是到了跟Spring MVC integrating的时候,遇到一点点有趣的事情. 这个 ...

  3. 流行python服务器框架

    流行python服务器框架   1.tonardo---- 多并发.轻量级应用, “非阻塞”的web 容器.类似tomcat.这个大家太熟悉了,就不多说了. 2.Twisted---- Twisted ...

  4. 以交互方式使用exp/imp的演示

    众所周知,用exp/imp对数据库进行逻辑备份.包含表.用户,整个数据库,我们通常所熟悉的是使用命令行指定參数的方式来做的.以下我来演示一下不太经常使用的以交互方式的操作,操作非常easy.就是仅仅要 ...

  5. EasyUI - ComboBox 下拉组件

    效果: html代码: <input id ="comb" name ="comb"/> JS代码: $(function () { $('#com ...

  6. CSS未知div高度垂直居中代码_层和布局特效

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. 基于visual Studio2013解决面试题之0707最小元素

     题目

  8. 通过IP或socket获取对方的MAC地址

    1.通过已经连接的socket文件获取: int getpeermac( int sockfd, char *buf ) { int ret =0; struct arpreq arpreq; str ...

  9. JQuery - 留言之后,不重新加载数据,直接显示发表内容

    留言板中,发表信息的时候,使用Ajax存储到后台数据库,如果存储成功,不重新加载数据库,直接显示发表内容. 代码: var Nicehng = ''; var kkimgpath = ''; var ...

  10. delphi不同版本字符串类型的演化(要支持基于firemonkey的app调用,字符串最好使用olevariant类型)

    string,DELPHI2009以前的版本string=ansistring,一个字符占一个字节,DELPHI2009及以上版本string=unicodestring,一个字符占二个字节. cha ...