#vec #thread #different #parts #writer #write #sharded

sharded-vec-writer

从不同线程写入 Vec 的部分

1 个不稳定版本

0.1.0 2024年8月10日

并发 中排名第 265

Download history 64/week @ 2024-08-04 58/week @ 2024-08-11

每月下载量 122

MIT/Apache

9KB
111

Sharded Vec Writer

此包旨在在您想要构建一个 Vec<T> 并让不同的线程初始化 vec 的不同部分时使用。

示例用法

use sharded_vec_writer::VecWriter;

// Create the vec with sufficient capacity for whatever we'd like to put in it.
let mut v = Vec::with_capacity(20);

// Create a writer - this mutably borrows the vec.
let mut writer: VecWriter<u32> = VecWriter::new(&mut v);

// Create however many shards we'd like, up to the capacity of the vec.
let mut shard1 = writer.take_shard(8);
let mut shard2 = writer.take_shard(2);
let mut shard3 = writer.take_shard(10);

// Write to the shards, possibly from multiple threads. Scoped threads help here.
std::thread::scope(|scope| {
    scope.spawn(|| {
        for i in 0..8 {
            shard1.push(i);
        }
    });
    scope.spawn(|| {
        for i in 8..10 {
            shard2.push(i);
        }
    });
});
for i in 10..20 {
    shard3.push(i);
}

// Return the shards to the writer. Shards must be fully initialised and must be returned in
// order.
writer.return_shard(shard1);
writer.return_shard(shard2);
writer.return_shard(shard3);

assert_eq!(v.len(), 20);
assert_eq!(v.capacity(), 20);
assert_eq!(v, (0..20).collect::<Vec<_>>());

许可协议

根据您的选择,在 Apache 许可证,版本 2.0MIT 许可证 下获得许可。

除非您明确声明,否则您有意提交给 Wild 的任何贡献,根据 Apache-2.0 许可证定义,应如上所述双重许可,不附加任何额外条款或条件。

无运行时依赖