GithubHelp home page GithubHelp logo

nabule / io-queue Goto Github PK

View Code? Open in Web Editor NEW

This project forked from esoma/io-queue

0.0 2.0 0.0 114 KB

A header only library that implements a C11 Unbounded Non-Intrusive Lockless Single Consumer Multiple Producer Thread-Safe FIFO Queue.

License: MIT License

C 100.00%

io-queue's Introduction

IO-Queue

IO-Queue is a header only library that implements a C11 Unbounded Non-Intrusive Lockless Single Consumer Multiple Producer FIFO Queue. It excels at taking I/O bound work from many threads and instead performing that I/O bound work on a seperate thread.

Usage

Simply include io_queue.h, there are no dependencies (other than a compiler that supports the atomic C11 standard library components).

Create a queue using the IoQueue structure:

IoQueue my_queue;
io_queue_init(&my_queue, sizeof(int));

To put data in the queue use the io_queue_push function. Pushing supports copying and moving. Pushing is considered a producer operation. Any thread can safely execute this operation at any time.

int in = 1;
io_queue_push(&my_queue, &in);

To check for data in the queue the io_queue_has_front function should be used. Checking the front is considered a consumer operation, only one thread may safely execute this at one time.

if (io_queue_has_front(&my_queue) == IO_QUEUE_RESULT_TRUE)
{
	// do something
}

To get data out of the queue the io_queue_front function is used. You should always check that there is data in queue before calling front as there is no built in check. If no data is in the queue when front is called a segfault is likely (or an assertion failure). Getting data is considered a consumer operation, only one thread may safely execute this at one time.

if (io_queue_has_front(&my_queue) == IO_QUEUE_RESULT_TRUE)
{
	int out;
	io_queue_front(&my_queue, &out);
}

To remove an item from the queue the io_queue_pop function is used. You should always check that there is data in queue before popping as there is no built in check. If no data is in the queue when pop is called a segfault is likely (or an assertion failure). Popping is considered a consumer operation, only one thread may safely execute this at one time.

if (io_queue_has_front(&my_queue) == IO_QUEUE_RESULT_TRUE)
{
	io_queue_pop(&my_queue);
}

Always make sure the queue is empty before letting it fall out of scope or freeing it, otherwise you'll leak memory. The io_queue_clear function is a convenient way to nuke the queue.

io_queue_clear(&my_queue);

Test

Tests are successful if there is no output. Example build command with gcc on windows:

gcc -ggdb -Wall test.c -o test.exe

io-queue's People

Contributors

esoma avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.