Skip to content

Make crossbeam::deque::Worker::push return slotted index#1246

Open
Bajix wants to merge 1 commit into
crossbeam-rs:mainfrom
Bajix:feature/push-index-swap
Open

Make crossbeam::deque::Worker::push return slotted index#1246
Bajix wants to merge 1 commit into
crossbeam-rs:mainfrom
Bajix:feature/push-index-swap

Conversation

@Bajix

@Bajix Bajix commented Apr 8, 2026

Copy link
Copy Markdown

See #743

It is useful to know when crossbeam::deque::Worker::push adds the first task to an empty deque as this can be used to facilitate simple batching by triggering tokio::task::spawn and work stealing to completion.

Here's an example of the how this change could be used

pub fn batch_update_cache<K, V>(runtime_handle: &Handle, data: (String, Vec<u8>)) {
  thread_local! {
    static QUEUE: Worker<(String, Vec<u8>)> = Worker::new_fifo();
  }

  QUEUE.with(|queue| {
    if queue.push(data).eq(&0) {
      let stealer = queue.stealer();

      runtime_handle.spawn(async move {
        let mut cache = vec![];

        cache.extend(std::iter::from_fn(|| loop {
          match stealer.steal() {
            Steal::Success(data) => break Some(data),
            Steal::Retry => continue,
            Steal::Empty => break None,
          }
        }));

        /// do something with cache
      });
    }
  });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

2 participants