The Crystal Programming Language Forum

Channel#empty?/full? gone

We have an existing code base that relies on Channel#full? and Channel#empty? but those methods are gone in 0.31.0. For example, in one part of the code we only want to fsync a file when the incoming queue is empty:

loop do
  file.write_bytes ch.receive
  file.fsync if ch.empty?
end

Is there a technical difficulty to re-implement the methods? Or do any one have suggestions on how to work around that?

This is what I did:

class Channel(T)
  def full?
    if queue = @queue
      return queue.size >= @capacity
    end

    !@senders.empty?
  end

  def empty?
    if queue = @queue
      return queue.empty?
    end

    @senders.empty?
  end
end

But others have pointed out that it’s not thread safe.

I cannot find the PR for this. What was the reason to remove it. It looks like it was removed around the time MT was merged in.

It was removed for the MT support. Let’s follow this discussion in Restoring Channel#full?