1
0
Fork 0
mirror of https://github.com/tailix/libshmemq.git synced 2025-02-17 15:45:41 -05:00

Solve the main problem, describe in README

This commit is contained in:
Alex Kotov 2020-12-14 20:10:09 +05:00
parent 841fd77530
commit 5884fa96e4
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08

View file

@ -192,6 +192,83 @@ zero):
7 - 0 = 7
```
So producer makes himself to point to the beginning of the buffer.
**NOTICE**: It marks the frame it pointed to earlier in a special way (indicated
with `*`). Remember this fact, we'll use it in future.
```
|
_V_ ___ ___ ___ ___ ___ ___ ___ ___ ___
| | | | | | | |16 |* | |
|___|___|___|___|___|___|___|___|___|___|
|
V
```
Consumer pops one message:
```
|
_V_ ___ ___ ___ ___ ___ ___ ___ ___ ___
| | | | | | | | |* | |
|___|___|___|___|___|___|___|___|___|___|
|
V
```
Producer pushes one message:
```
|
___ _V_ ___ ___ ___ ___ ___ ___ ___ ___
|17 | | | | | | | |* | |
|___|___|___|___|___|___|___|___|___|___|
|
V
```
Consumer is also in trouble now: how can he understand if he should try to read
next frame or go the beginning of the buffer? We can differentiate those
situations because we indicate consumed messages with no number, but in reality
corresponding frames contain grabage.
But why not to fill those frames with special value? First frame of a message
contains the number of frames in the message. It can't be zero. Can we set it to
zero for consumed frames?
Let's start with a caution. Message can consist of multiple frames, and only the
first one will have such attribute. Can we guarantee that consumer is pointed to
the frame which was the first frame of some consumed message? No, we can't.
However we can overwrite garbage when producer decides to start from the
beginning of the buffer. Do you remember the **NOTICE**? We only consider this
when position of producer is less than position of consumer. In this case if we
see frame with size greater than zero, it unequivocally signs that there is a
message, not a garbage.
Consumer makes himself to point to the beginning of the buffer:
```
|
___ _V_ ___ ___ ___ ___ ___ ___ ___ ___
|17 | | | | | | | | | |
|___|___|___|___|___|___|___|___|___|___|
|
V
```
Consumer pops one message:
```
|
___ _V_ ___ ___ ___ ___ ___ ___ ___ ___
| | | | | | | | | | |
|___|___|___|___|___|___|___|___|___|___|
|
V
```
External links