-
Notifications
You must be signed in to change notification settings - Fork 0
psantacl/clj-ring-buffer
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
# clj-ring-buffer
A lockless ring buffer supporting an arbitrary number of producers and
consumer. This is a VERY fast mechanism for inter-thread communication.
## Usage
(make-ring-buffer num-of-producers num-of-consumers size-of-ring-buffer))
make-ring-buffer returns a map containing producer functions, consumer functions, and the underlying ring-buffer itself.
(let [{producer-1 :producer-1 consumer-1 :consumer-1 producer-2 :producer-2 consumer-2 :consumer-2
r-buffer :ring-buffer}
(rb/make-ring-buffer 2 2 4)]
(producer-1 "A")
;;will return false if the buffer is full with respect to
;;producer-1 and the data could not be inserted into the ring buffer.
;;In such a case, the production should be retried by the client.
(producer-2 "B")
(consumer-1)
;;returns "A"
(empty-buffer? r-buffer 1)
;;returns true. Buffer is empty for consumer-1
(consumer-2)
;;returns "B"
(empty-buffer? r-buffer 2)
;;returns true. Buffer is empty for consumer-2
;;view the contents of the ring buffer
(peek r-buffer)
;;("A","B", nil, nil)
## License
Information Wants To Be Free
About
Don't use you locks for interthread communication. Use ring buffers. :)
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published