Skip to content

Don't use you locks for interthread communication. Use ring buffers. :)

Notifications You must be signed in to change notification settings

psantacl/clj-ring-buffer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

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

No packages published