0

我正在使用一个包含复杂属性的类。这些属性中的每一个都是通过不同的方法计算的。我Parallel.Invoke用来更新同一个对象的不同属性。这会对对象造成任何问题吗?

// sample class definition. I've simplified the example by using 'object' type
// for complex types. 
public class TestResult
{
     public object Property1;

     public object Property2;

     public object Property3;
}

// here we populate an object. We are processing it parallelly because each method
// takes some considerable amount of time. 
var testResult = new TestResult();
Parallel.Invoke(
() =>
{
       testResult.Property1 = GetProperty1Value();
},
() =>
{
       testResult.Property2 = GetProperty2Value();
},
() =>
{
       testResult.Property3 = GetProperty3Value();
});

上面的代码会导致任何问题testResult吗?

注意:我已经测试了这部分代码。似乎不会引起任何问题。据我所知,由于不同的属性在不同的任务中得到处理,这应该不是问题。我找不到与此相关的任何文档。我想确认这种行为,因此提出了这个问题。

4

1 回答 1

1

首先应该提到的是Property1,在您的示例中,Property2Property3在技术上称为字段,而不是属性

在操作成功完成TestResult后,您的示例在实例的完整性方面是完全安全的。Parallel.Invoke它的所有字段都将被初始化,并且它们的值将对当前线程可见(但不一定对在完成之前已经运行的其他线程可见Parallel.Invoke)。

另一方面,如果Parallel.Invoke失败,则TestResult实例可能最终被部分初始化。

如果Property1,Property2Property3实际上是properties,那么代码的线程安全性将取决于在set这些属性的访问器后面运行的代码。如果此代码是微不足道的,例如set { _property1 = value; },那么您的代码将是安全的。

作为旁注,建议您Parallel.Invoke使用合理的MaxDegreeOfParallelism. 否则,您将获得Parallel该类的默认行为,即使ThreadPool.

TestResult testResult = new();

Parallel.Invoke(new ParallelOptions()
{ MaxDegreeOfParallelism = Environment.ProcessorCount },
    () => testResult.Property1 = GetProperty1Value(),
    () => testResult.Property2 = GetProperty2Value(),
    () => testResult.Property3 = GetProperty3Value()
);

替代方案:如果您想知道如何在TestResult不依赖闭包和副作用的情况下初始化实例,这是一种方法:

var taskFactory = new TaskFactory(new ConcurrentExclusiveSchedulerPair(
    TaskScheduler.Default, Environment.ProcessorCount).ConcurrentScheduler);

var task1 = taskFactory.StartNew(() => GetProperty1Value());
var task2 = taskFactory.StartNew(() => GetProperty2Value());
var task3 = taskFactory.StartNew(() => GetProperty3Value());

Task.WaitAll(task1, task2, task3);

TestResult testResult = new()
{
    Property1 = task1.Result,
    Property2 = task2.Result,
    Property3 = task3.Result,
};

属性的值临时存储在各个Task对象中,最后在所有任务完成后,在当前线程上将它们分配给属性。TestResult因此,这种方法消除了有关构造实例完整性的所有线程安全考虑。

但是有一个缺点:它Parallel.Invoke利用了当前线程,并且也调用了它的一些动作。相反,这种Task.WaitAll方法会浪费地阻塞当前线程,让线程ThreadPool完成所有工作。


只是为了好玩:我尝试编写一个ObjectInitializer工具,它应该能够并行计算对象的属性,然后按顺序(线程安全地)分配每个属性的值,而不必手动管理一堆分散的Task变量。这是我想出的API:

var initializer = new ObjectInitializer<TestResult>();
initializer.Add(() => GetProperty1Value(), (x, v) => x.Property1 = v);
initializer.Add(() => GetProperty2Value(), (x, v) => x.Property2 = v);
initializer.Add(() => GetProperty3Value(), (x, v) => x.Property3 = v);
TestResult testResult = initializer.RunParallel(degreeOfParallelism: 2);

不是很漂亮,但至少它是简洁的。该Add方法为一个属性添加元数据,并RunParallel执行并行和顺序工作。这是实现:

public class ObjectInitializer<TObject> where TObject : new()
{
    private readonly List<Func<Action<TObject>>> _functions = new();

    public void Add<TProperty>(Func<TProperty> calculate,
        Action<TObject, TProperty> update)
    {
        _functions.Add(() =>
        {
            TProperty value = calculate();
            return source => update(source, value);
        });
    }

    public TObject RunParallel(int degreeOfParallelism)
    {
        TObject instance = new();
        _functions
            .AsParallel()
            .AsOrdered()
            .WithDegreeOfParallelism(degreeOfParallelism)
            .Select(func => func())
            .ToList()
            .ForEach(action => action(instance));
        return instance;
    }
}

它使用PLINQ而不是Parallel类。

我会用它吗?可能不是。主要是因为并行初始化对象的需求并不经常出现,并且在这种罕见的情况下必须维护如此晦涩的代码似乎有点过头了。我可能会改用肮脏和副作用的Parallel.Invoke方法。:-)

于 2021-09-11T09:25:21.100 回答