# Feedback on a Threadsafe Buffer Manager

**URL:** <https://discourse.julialang.org/t/feedback-on-a-threadsafe-buffer-manager/104782>\
**Category:** General Usage\
**Created:** [October 10, 2023, 3:32am UTC](https://discourse.julialang.org/t/feedback-on-a-threadsafe-buffer-manager/104782 "2023-10-10T03:32:26Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [October 10, 2023, 3:32am UTC](https://discourse.julialang.org/t/feedback-on-a-threadsafe-buffer-manager/104782/1 "2023-10-10T03:32:26Z")

</div>

In [this conversation](https://discourse.julialang.org/t/loops-allocations-and-helping-the-coder/104733/17), I mentioned that I used a Channel to manage preallocated objects over the lifetime of a pipeline.

I was hoping someone could offer some feedback on my approach to that:

```julia
"""
    get_buffer_manager(size_hint=0)
Return a channel of IOBuffers. It will always have a fresh buffer ready, and can hold returned
buffers to allow reuse
"""
function get_buffer_manager(size_hint=0)
    return Channel{IOBuffer}(Threads.nthreads()+1) do ch
        while isopen(ch)
            if !isready(ch)
                @info "creating a new IOBuffer"
                put!(ch, IOBuffer(;sizehint=size_hint))
            end
            sleep(0.5)
        end
    end
end

```

I can then `take` a new `IOBuffer` at the top of the pipeline and then return it with `put` in the final step of the pipeline. It works nicely, but it feels silly to have it do this sleep/while loop thing to check if the channel is empty. Is there a blocking way to wait for a channel to be empty/ready? Is there a better way to do what I’m doing here?
