-
Notifications
You must be signed in to change notification settings - Fork 1
13 Thread Safety.md
cyclonedll edited this page Jun 2, 2026
·
1 revision
QuiverSet<TEntity> internally uses ReaderWriterLockSlim to implement reader-writer separation:
flowchart LR
subgraph "Read Operations (Shared Lock)"
S["Search"]
F["Find"]
EX["Exists"]
C["Count"]
FE["foreach / LINQ"]
GA["GetAll"]
end
subgraph "Write Operations (Exclusive Lock)"
A["Add"]
AR["AddRange"]
U["Upsert"]
R["Remove"]
CL["Clear"]
LE["LoadEntities"]
end
S & F & EX & C & FE & GA -->|"Parallel execution ✅"| RLock["EnterReadLock"]
A & AR & U & R & CL & LE -->|"Mutually exclusive 🔒"| WLock["EnterWriteLock"]
var db = new MyDocumentDb();
// ✅ Safe: multi-threaded concurrent search (shared read lock)
var tasks = Enumerable.Range(0, 24).Select(_ => Task.Run(() =>
{
var query = GenerateRandomVector(384);
return db.Documents.Search(e => e.Embedding, query, topK: 5);
}));
await Task.WhenAll(tasks);
// ✅ Safe: concurrent read-write (read operations wait while write holds exclusive lock)
var writerTask = Task.Run(() =>
{
db.Documents.Upsert(new Document
{
Id = "new-doc",
Title = "New Document",
Embedding = new float[384]
});
});
var readerTask = Task.Run(() =>
db.Documents.Search(e => e.Embedding, queryVector, topK: 5));
await Task.WhenAll(writerTask, readerTask);QuiverSet uses Interlocked.Exchange(ref _disposed, 1) to guarantee concurrent Dispose safety. All operation entry points call ThrowIfDisposed(), using Volatile.Read to ensure cross-thread visibility.
| Test Scenario | Data Size | Configuration | Result |
|---|---|---|---|
| Pure read concurrency | 3,000 entries × 3 vectors | 24 threads × 100 searches | 2,400 searches with zero exceptions |
| Mixed read-write | 1,000 entries × 3 vectors | 4 writers + 8 readers + 2 deleters, 3 seconds | Zero exceptions |
| Batch write + search | Dynamically growing | 3 writer threads (50 per batch) + 6 search threads, 3 seconds | Zero exceptions |
| # | 章节 |
|---|---|
| 01 | 版本说明 |
| 02 | 产品概述 |
| 03 | 架构概述 |
| 04 | 快速开始 |
| 05 | 核心概念 |
| 06 | 距离度量 |
| 07 | 索引类型 |
| 08 | CRUD 操作 |
| 09 | 向量搜索 |
| 10 | 持久化存储 |
| 11 | 迁移系统 |
| 11a | 模式迁移 |
| 12 | 多向量字段支持 |
| 13 | 线程安全与并发 |
| 14 | 生命周期管理 |
| 15 | 配置选项 |
| 16 | 内部实现细节 |
| 17 | 完整示例 |
| 18 | API 参考速查表 |
| 19 | 使用建议 |