在运行期间,我可以在TransfromBlock中“热交换”一个Func吗?

时间:2013-02-16 08:49:42

标签: c# task-parallel-library func tpl-dataflow

我有TransformBlock的以下设置:

private void SetupTestModule()
{
    Func<int, int> func1 = new Func<int, int>(input =>
        {
            return (input + 1);
        });

    Func<int, int> func2 = new Func<int, int>(input =>
        {
            return (input + 2);
        });

    TransformBlock<int, int> transform = new TransformBlock<int, int>(func1);
}

我想知道在运行期间是否可以为func2交换func1以及此transformBlock和其他数据块之间的所有链接是否都将保持不变?理想情况下,在交换Funcs时,我只希望将新转换应用于所有新传入的项目。显然,使用这种简单的方法,我不会假设当前在队列中的项目以及如何处理它们。我只是想知道分配新的Func是否会导致运行时错误或者导致transformBlock被取消链接。谁可以分享一些见解?

编辑:在这里发布Jon的建议和代码一些非常基本的测试代码。让我好奇的是它可以使用和不使用volatile关键字。我们为什么需要volatile

public class TransformBlockHotSwap
{
    private TransformBlock<int, int> transformBlock;
    private ActionBlock<int> actionBlock;

    public TransformBlockHotSwap()
    {

        SwappableFunction<int, int> swappable = new SwappableFunction<int, int>(item => item + 1);
        transformBlock = new TransformBlock<int, int>(item => swappable.Execute(item));
        actionBlock = new ActionBlock<int>(item => Console.WriteLine(item));
        transformBlock.LinkTo(actionBlock);

        Func<int, int> func2 = new Func<int,int>(item => item * item);

        for (int index = 1; index <= 100; index++)
        {
            transformBlock.Post(index);

            Thread.Sleep(500);

            if (index == 5)
            {
                swappable.Swap(func2);
            }
        }
    }
}

public class SwappableFunction<TInput, TOutput>
{
    private Func<TInput, TOutput> func;

    public SwappableFunction(Func<TInput, TOutput> func)
    {
        this.func = func;
    }

    public void Swap(Func<TInput, TOutput> newFunc)
    {
        func = newFunc;
    }

    public TOutput Execute(TInput input)
    {
        return func(input);
    }
}

编辑(包括谓词交换):

public class TransformBlockHotSwap
{
    private TransformBlock<int, int> transformBlock;
    private ActionBlock<int> actionBlock;

    public TransformBlockHotSwap()
    {
        Func<int, int> defaultFunction = new Func<int, int>(item => item);
        Func<int, int> func2 = new Func<int, int>(item => item * item);
        Predicate<int> defaultPredicate = new Predicate<int>(item => true);
        Predicate<int> pred2 = new Predicate<int>(item =>
            {
                if (item % 2 == 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            });

        SwappableFunction<int, int> swappableFunction = new SwappableFunction<int, int>(defaultFunction);
        SwappablePredicate<int> swappablePredicate = new SwappablePredicate<int>(defaultPredicate);

        transformBlock = new TransformBlock<int, int>(item => swappableFunction.Execute(item));
        actionBlock = new ActionBlock<int>(item => Console.WriteLine(item));
        transformBlock.LinkTo(actionBlock, item => swappablePredicate.Execute(item));

        for (int index = 1; index <= 100; index++)
        {
            transformBlock.Post(index);

            if (index == 10)
            {
                swappablePredicate.Swap(pred2);
            }

            Thread.Sleep(200);
        }

        Console.WriteLine("Done");
        Console.ReadKey();
    }
}

public class SwappableFunction<TInput, TOutput>
{
    private volatile Func<TInput, TOutput> func;

    public SwappableFunction(Func<TInput, TOutput> defaultFunction)
    {
        this.func = defaultFunction;
    }

    public void Swap(Func<TInput, TOutput> newFunc)
    {
        func = newFunc;
    }

    public TOutput Execute(TInput input)
    {
        return func(input);
    }
}

public class SwappablePredicate<TInput>
{
    private volatile Predicate<TInput> predicate;

    public SwappablePredicate(Predicate<TInput> defaultPredicate)
    {
        this.predicate = defaultPredicate;
    }

    public void Swap(Predicate<TInput> newPredicate)
    {
        predicate = newPredicate;
    }

    public bool Execute(TInput input)
    {
        return predicate(input);
    }
}

1 个答案:

答案 0 :(得分:3)

我不希望你能够这样做 - 但你可以轻松地一个委托的函数:

public class SwappableFunction<TInput, TOutput>
{
    private volatile Func<TInput, TOutput> func;

    public SwappableFunction(Func<TInput, TOutput> func)
    {
        this.func = func;
    }

    public void Swap(Func<TInput, TOutput> newFunc)
    {
        func = newFunc;
    }

    public TOutput Execute(TInput input)
    {
        return func(input);
    }
}

然后:

var swappable = new SwappableFunction<int, int>(input => input + 1);
var block = new TransformBlock<int, int>(swappable.Execute);
// Later...

swappable.Swap(input => input + 2);

重要 - 我不是100%肯定在这里使用volatile - 我通常不喜欢使用volatile,因为它的语义令人困惑。使用Interlocked.CompareExchange可能会更好 - 但代码会更长,我希望首先得到主要观点:)