非并发集合在并发集合中安全吗?

Efi*_*fie 3 .net collections concurrency powershell thread-safety

我希望开始在我正在从事的项目中实现一些并发功能。我最近发现了System.Collections.Concurrent我计划利用的命名空间。

我用来跟踪操作整体状态的对象本质上是一个带有一些嵌套自定义对象的字典。我的想法是,只要将最高级别的集合配置为并发/线程安全,嵌套集合是否是并发/线程安全并不重要,因为数据将被更高级别的集合锁定。

这是正确的假设吗?

例如,像下面这样的 PowerShell 中的某些内容可以安全使用吗?

[System.Collections.Concurrent.ConcurrentDictionary[[String], [MyCustomClass]]]::new()
Run Code Online (Sandbox Code Playgroud)

此外,我有一些扩展 HashSet 的自定义类以避免重复。由于 System.Collections. Concurrent 没有 HashSet 类,获得类似功能但并发的推荐方法是什么?

Mat*_*sen 6

我的想法是,只要将最高级别的集合配置为并发/线程安全,嵌套集合是否是并发/线程安全并不重要,因为数据将被更高级别的集合锁定。

这是正确的假设吗?

不,这不是一个安全的假设。

假设您创建一个包含一堆常规哈希表的并发字典:

using namespace System.Collections.Concurrent

# Create thread-safe dictionary
$rootDict = [ConcurrentDictionary[string,hashtable]]::new()
Run Code Online (Sandbox Code Playgroud)
  • $rootDict现在是线程安全的 - 多个线程无法'A'通过覆盖对哈希表的引用来同时修改条目
  • 我们添加到的任何内部哈希表都不$rootDict是线程安全的 - 它仍然只是一个常规哈希表

ForEach-Object -Parallel在 PowerShell 7 中,当使用操作这样的数据结构时可以观察到这一点:

using namespace System.Collections.Concurrent

# Create thread-safe dictionary
$rootDict = [ConcurrentDictionary[string,hashtable]]::new()

1..100 |ForEach-Object -Parallel {
  # We need a reference to our safe top-level dictionary
  $dict = $using:rootDict

  # ... and we need a key
  $rootKey = $_ % 2 -eq 0 ? 'even' : 'odd'

  # Thread-safe acquisition of inner hashtable
  $innerDict = $dict.GetOrAdd($rootKey, {param($key) return @{}})

  # Add a bit of jitter for realism
  Start-Sleep -Milliseconds (Get-Random -Minimum 50 -Maximum 250)

  # Update inner hashtable entry
  $innerDict['Counter'] += 1
} -ThrottleLimit 10

# Are these really the results we're expecting...? 
$rootDict['odd','even']
Run Code Online (Sandbox Code Playgroud)

如果内部哈希表条目是线程安全的,可以同时更新,您会期望两个计数器都位于50,但我在笔记本电脑上得到如下结果:

using namespace System.Collections.Concurrent

# Create thread-safe dictionary
$rootDict = [ConcurrentDictionary[string,hashtable]]::new()

1..100 |ForEach-Object -Parallel {
  # We need a reference to our safe top-level dictionary
  $dict = $using:rootDict

  # ... and we need a key
  $rootKey = $_ % 2 -eq 0 ? 'even' : 'odd'

  # Thread-safe acquisition of inner hashtable
  $innerDict = $dict.GetOrAdd($rootKey, {param($key) return @{}})

  # Add a bit of jitter for realism
  Start-Sleep -Milliseconds (Get-Random -Minimum 50 -Maximum 250)

  # Update inner hashtable entry
  $innerDict['Counter'] += 1
} -ThrottleLimit 10

# Are these really the results we're expecting...? 
$rootDict['odd','even']
Run Code Online (Sandbox Code Playgroud)

我们可以看到内部“计数器”条目的多个更新在此过程中丢失,大概是由于并发更新所致。


为了测试这个假设,让我们做同样的实验,但使用另一种并发字典类型而不是哈希表:

using namespace System.Collections.Concurrent

# Create thread-safe dictionary with a thread-safe item type
$rootDict = [ConcurrentDictionary[string,ConcurrentDictionary[string,int]]]::new()

1..100 |ForEach-Object -Parallel {
  # We need a reference to our safe top-level dictionary
  $dict = $using:rootDict

  # ... and we need a key
  $rootKey = $_ % 2 -eq 0 ? 'even' : 'odd'

  # Thread-safe acquisition of inner hashtable
  $innerDict = $dict.GetOrAdd($rootKey, {param($key) return @{}})

  # Add a bit of jitter for realism
  Start-Sleep -Milliseconds (Get-Random -Minimum 50 -Maximum 250)

  # Thread-safe update of inner dictionary
  [void]$innerDict.AddOrUpdate('Counter', {param($key) return 1}, {param($key,$value) return $value + 1})
} -ThrottleLimit 10

# These should be the exact results we're expecting! 
$rootDict['odd','even']
Run Code Online (Sandbox Code Playgroud)

现在我得到:

Name                           Value
----                           -----
Counter                        46
Counter                        43
Run Code Online (Sandbox Code Playgroud)

我有一些扩展 HashSet 的自定义类以避免重复。由于 System.Collections. Concurrent 没有 HashSet 类,获得类似功能但并发的推荐方法是什么?

HashSet我强烈建议您包装a HashSet,然后用 a 保护您想要向用户公开的所有方法,而不是显式继承自ReaderWriterLockSlim,这样您就可以实现线程安全,而无需不必要地牺牲读取访问性能。

在这里,反对使用[int]作为示例日期类型:

using namespace System.Collections.Generic
using namespace System.Threading

class ConcurrentSet
{
    hidden [ReaderWriterLockSlim]
    $_lock

    hidden [HashSet[int]]
    $_set

    ConcurrentSet()
    {
        $this._set = [HashSet[int]]::new()
        $this._lock = [System.Threading.ReaderWriterLockSlim]::new()
    }

    [bool]
    Add([int]$item)
    {
        # Any method that modifies the set should be guarded
        # by a WriteLock - guaranteeing exclusive update access
        $this._lock.EnterWriteLock()
        try{
            return $this._set.Add($item)
        }
        finally{
            $this._lock.ExitWriteLock()
        }
    }

    [bool]
    IsSubsetOf([IEnumerable[int]]$other)
    {
        # For the read-only methods a read-lock will suffice
        $this._lock.EnterReadLock()
        try{
            return $this._set.IsSubsetOf($other)
        }
        finally{
            $this._lock.ExitReadLock()
        }
    }

    # Repeat appropriate lock pattern for all [HashSet] methods you want to expose
}
Run Code Online (Sandbox Code Playgroud)

您可以通过包装 a并使用HashSet<object>自定义比较器控制行为来使包装器更加灵活