Creating a threading queue with a timer in C for * nix

One part of my program creates some kind of messages. These messages are then processed in the second part.

I need some kind of temporary queue between parts of my program, which can store messages in memory for X seconds. X does not change as long as such a queue of time exists.

Ideally, it should look like this:

tqueue_t *tqueue_new(int seconds); int tqueue_push(tqueue_t *queue, void *msg); void *tqueue_pop(tqueue_t *queue); 

tqueue_pop() should block and return when the first message has been in the queue for X seconds.

What is the best way to do this? Perhaps there are already existing solutions?

Language: C

OS: * nix

In addition, this queue should work in a streaming environment.

+4
source share
1 answer

You can build this on top of POSIX Message Queues and let it take care of most of the details. Sort of:

(1) recording queue with timestamp field

(2) In another thread, either block mq_receive () until a message appears (or use mq_notify () to submit or create a thread for you).

(3) Read the line and mark the time stamp.

(4) Calculate the time difference, how long you should wait and use select () or some kind of sleep / timer mechanism.

(5) Process the message.

+2
source

Source: https://habr.com/ru/post/1335983/


All Articles