1 Star 1 Fork 1

ClarePhang / message_queue

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
BSD-3-Clause

Introduction

I've always longed for a good, fast way to relay information between threads. So, I built one!

What's a message? It's anything you want it to be: a built-in data type, a character string, a function pointer, or a complicated data structure. The queue won't copy or move your structure, so internal pointers remain intact. A message is anything your application wants to send between threads.

How does it work?

The library uses a lock-free allocator to allocate memory for messages. Then, your application can construct whatever it needs to send in-place. When you write the message to the queue, it's added to a lock-free structure similar to the one used to allocate memory.

Why should I use this?

  • It's fast. Crazy fast. My three-year-old laptop can push around 6,500,000 messages per second between threads, including the overhead of allocating the messages.

  • It's easy. There are only 8 functions to learn, and you probably only need 6 of them. Really, there are only 3 concepts to worry about:

    • initialization/teardown,
    • allocation/deallocation, and
    • writing/reading.

    If you're a C programmer, you've dealt with all of these already.

Why shouldn't I use this?

  • It's new and so not widely tested. In fact, it's only been tested at all on two x86_64 machines, running Mac OS X and Linux.
  • It may be overly tuned to my Core 2 Duo. Performance is tricky and often very hardware dependent. Hopefully wider exposure will help this work well on a wider variety of hardware.
  • I have no clue how well it scales past two CPUs. Anyone want to try it on a bigger, beefier machine?
  • You have to know how big the largest message you want to send on a given queue is in advance, and you have to decide on a maximum depth the queue can reach.

How do I use this?

First, set up a message queue somewhere:

struct message_queue queue;

Before using it, you have to initialize it:

message_queue_init(&queue, 512, 128); /* The biggest message we'll send
                                       * with this queue is 512 bytes, and
                                       * the queue can only be 128
                                       * messages deep */

To send a message:

struct my_message *message = message_queue_message_alloc_blocking(&queue);
/* Construct the message here */
message_queue_write(&queue, message);

Or, if you'd rather discard your message if there's no free memory in the queue:

struct my_message *message = message_queue_message_alloc(&queue);
if(message) {
    /* Construct the message here */
    message_queue_write(&queue, message);
}

To read a message:

/* Blocks until a message is available */
struct my_message *message = message_queue_read(&queue);
/* Do something with the message here */
message_queue_message_free(&queue, message);

If you'd rather not block to wait for a new message:

/* Returns NULL if no message is available */
struct my_message *message = message_queue_tryread(&queue);
if(message) {
    /* Do something with the message here */
    message_queue_message_free(&queue, message);
}

Whenever you're done with the queue (and no other threads are accessing it anymore):

message_queue_destroy(&queue);

So give it a shot and let me know what you think!

Copyright (c) 2012 Jeremy Pepper All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of message_queue nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

简介

sync https://github.com/LnxPrgr3/message_queue.git 展开 收起
C 等 2 种语言
BSD-3-Clause
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C
1
https://gitee.com/ClarePhang/message_queue.git
git@gitee.com:ClarePhang/message_queue.git
ClarePhang
message_queue
message_queue
master

搜索帮助