# Pushfirst! for Channel

**URL:** https://discourse.julialang.org/t/pushfirst-for-channel/83113
**Category:** General Usage
**Tags:** question
**Created:** [June 21, 2022, 10:03am UTC](https://discourse.julialang.org/t/pushfirst-for-channel/83113 "2022-06-21T10:03:44Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)
#### Post date: [June 21, 2022, 10:03am UTC](https://discourse.julialang.org/t/pushfirst-for-channel/83113/1 "2022-06-21T10:03:44Z")

</div>

Hi all 🙂

Is there an reason why the is no pushfirst! for Channel?

Cheers

---

<div class="post-metadata">

### Author: ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)
#### Post date: [June 21, 2022, 10:31am UTC](https://discourse.julialang.org/t/pushfirst-for-channel/83113/2 "2022-06-21T10:31:28Z")

</div>

For the buffered version it should be something like this:

```julia
julia> function Base.pushfirst!(c::Channel, v)
           lock(c)
           try
               while length(c.data) == c.sz_max
                   check_channel_state(c)
                   wait(c.cond_put)
               end
               pushfirst!(c.data, v)
               # notify all, since some of the waiters may be on a "fetch" call.
               notify(c.cond_take, nothing, true, false)
           finally
               unlock(c)
           end
           return v
       end

```

---

<div class="post-metadata">

### Author: ![anon56330260](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon56330260](https://discourse.julialang.org/u/anon56330260)
#### Post date: [June 21, 2022, 10:49am UTC](https://discourse.julialang.org/t/pushfirst-for-channel/83113/3 "2022-06-21T10:49:57Z")

</div>

But what’s the use of `pushfirst!` on Channel? It seems that this breaks Channel’s abstraction and doesn’t make sense… You can’t even guarantee this pushfirst! will insert element in a certain place.

---

<div class="post-metadata">

### Author: ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)
#### Post date: [June 21, 2022, 11:02am UTC](https://discourse.julialang.org/t/pushfirst-for-channel/83113/4 "2022-06-21T11:02:12Z")

</div>

I use it in a event based system. Events will queue up. Now an very important event happens (for example most current data). I want to process that as soon as possible.

---

<div class="post-metadata">

### Author: ![anon56330260](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon56330260](https://discourse.julialang.org/u/anon56330260)
#### Post date: [June 21, 2022, 11:29am UTC](https://discourse.julialang.org/t/pushfirst-for-channel/83113/5 "2022-06-21T11:29:22Z")

</div>

It seems that you need a concurrent priority queue in this case (with 2 priorities). Julia’s Channel is an ordinary queue so it’s quite natural that it doesn’t support `pushfirst!` out of box…Maybe you have to implement your own concurrent priority queue?

---

<div class="post-metadata">

### Author: ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)
#### Post date: [June 21, 2022, 12:18pm UTC](https://discourse.julialang.org/t/pushfirst-for-channel/83113/6 "2022-06-21T12:18:19Z")

</div>

Oki 🙂 but would it work if I add the function above?

---

<div class="post-metadata">

### Author: ![anon56330260](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon56330260](https://discourse.julialang.org/u/anon56330260)
#### Post date: [June 21, 2022, 12:54pm UTC](https://discourse.julialang.org/t/pushfirst-for-channel/83113/7 "2022-06-21T12:54:49Z")

</div>

I think the code you post is adopted Julia’s `put_bufferred!` implementation? This looks fine to me. But it seems that if there are already some writers queuing (that is, the queue is already full), pushfirst! will happen after these writers (since the condition variable also maintain a task queue to ensure FIFO order). So you might also want to check that part of code.

---

<div class="post-metadata">

### Author: ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)
#### Post date: [June 21, 2022, 1:38pm UTC](https://discourse.julialang.org/t/pushfirst-for-channel/83113/8 "2022-06-21T13:38:53Z")

</div>

Good point
