小编Nic*_*ico的帖子

C#并行读取访问List而不复制

请考虑以下示例:

class Example
{
    private readonly List<string> _list = new List<string>();
    private readonly object _lock = new object();

    public IReadOnlyList<string> Contents
    {
        get
        {
            lock (_lock)
            {
                return new List<string>(_list);
            }
        }
    }

    public void ModifyOperation(string example)
    {
        lock (_lock)
        {
            // ...
            _list.Add(example);
            // ...
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如何Contents在不复制整个List的情况下实现对List的并行读访问?在C#中有并发Collections,但没有线程安全List.在Java中有类似的东西CopyOnWriteArrayList.

.net c# parallel-processing

7
推荐指数
1
解决办法
318
查看次数

标签 统计

.net ×1

c# ×1

parallel-processing ×1