How do I implement a circular list (ring buffer) in C?

A very simple implementation, expressed in C. Implements a circular buffer style FIFO queue. Could be made more generic by creating a structure containing the queue size, queue data, and queue indexes (in and out), which would be passed in with the data to add or remove from the queue. These same routines could then … Read more

A non-nested animation sequence in jQuery?

You can make a custom .queue() to avoid the limitless nesting.. var q = $({}); function animToQueue(theQueue, selector, animationprops) { theQueue.queue(function(next) { $(selector).animate(animationprops, next); }); } // usage animToQueue(q, ‘#first’, {width: ‘+=100’}); animToQueue(q, ‘#second’, {height: ‘+=100’}); animToQueue(q, ‘#second’, {width: ‘-=50’}); animToQueue(q, ‘#first’, {height: ‘-=50’}); Demo at http://jsfiddle.net/gaby/qDbRm/2/ If, on the other hand, you want to … Read more

Insert into an STL queue using std::copy

Unfortunately std::queue ‘adapts’ the function known as push_back to just push which means that the standard back_insert_iterator doesn’t work. Probably the simplest way (albeit conceptually ugly) is to adapt the container adapter with a short lived container adapter adapter[sic] (eugh!) that lives as long as the back insert iterator. template<class T> class QueueAdapter { public: … Read more

Is there a queue implementation?

In fact, if what you want is a basic and easy to use fifo queue, slice provides all you need. queue := make([]int, 0) // Push to the queue queue = append(queue, 1) // Top (just get next element, don’t remove it) x = queue[0] // Discard top element queue = queue[1:] // Is empty … Read more

LinkedBlockingQueue vs ConcurrentLinkedQueue

For a producer/consumer thread, I’m not sure that ConcurrentLinkedQueue is even a reasonable option – it doesn’t implement BlockingQueue, which is the fundamental interface for producer/consumer queues IMO. You’d have to call poll(), wait a bit if you hadn’t found anything, and then poll again etc… leading to delays when a new item comes in, … Read more

Signal queuing in C

What happens is the following: First signal received, namely SIGUSR1, handler is called and is running Second signal received, since handler from nr1 is still running, the signal nr2 gets pending and blocked. Third signal received, since handler from nr1 is still running, the signal 3 gets discarded. Fourth, fifth…etc signal of the same type … Read more