AtomicChannel
AtomicChannel is a bounded, lock-free, multi-producer multi-consumer channel built on atomic operations.
Create a channel
using AtomicChannels
chnl = AtomicChannel{Int}(4)The channel capacity is fixed. Producers block on put! when full, and consumers block on take! when empty.
Core operations
put!(chnl, item): blocking pushtake!(chnl): blocking poptryput!(chnl, item): non-blocking push, returnsBooltrytake!(chnl): non-blocking pop, returns item ornothing
Example
chnl = AtomicChannel{Int}(2)
put!(chnl, 10)
put!(chnl, 20)
ok = tryput!(chnl, 30) # false, channel is full
x = take!(chnl) # 10
y = trytake!(chnl) # 20
z = trytake!(chnl) # nothingCompatibility helpers
AtomicChannel implements common Base channel-style utilities:
- Other non-blocking operations:
empty!(::AtomiChannel)- iterations like for loop, collect, etc.
- Waiting and peeking:
wait(::AtomicChannel)fetch(::AtomicChannel): wait and peek the next item but not take it away from AtomicChannel.
- State and capacity:
isready(::AtomicChannel)isempty(::AtomicChannel)isfull(::AtomicChannel)length(::AtomicChannel)
- Channel compatibility:
isopen(::AtomicChannel) = trueclose(::AtomicChannel) = nothing, etc
Notes
trytake!usesnothingto mean "empty". If item can benothing, prefer blockingtake!when ambiguity matters.- Internal ring indices are periodically wrapped to avoid long-running counter growth.