向已定义的屏障注册新线程 [英] Register new thread to already defined barrier

查看:45
本文介绍了向已定义的屏障注册新线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法向已经预定义的屏障添加线程?

Is there a way to add a thread to already pre-defined barrier?

场景:我在某个时间点有 N 个线程,代码声明了 Barrier 以便处理它们.

The scenario: I have at certain point of time N threads, and the code declares the Barrier in order to handle them.

问题是,有时我可能需要在该屏障实例内处理另一个新线程,但该屏障已经仅用 N 个线程声明.

The problem is, that sometimes I may need another new thread to be handled inside that barrier instance, but the barrier has already been declared with N threads only.

示例:

barrier = new Barrier(N, (sprint) => { 
       Console.WriteLine($"Current sprint: {sprint.CurrentPhaseNumber}")
});

声明后我需要用 N+1 个线程以某种方式再次更新它,有什么建议吗?

After the declaration I need to update it again somehow with N+1 threads, any suggestions?

推荐答案

关于 Barrier 的完整文档在这里:https://docs.microsoft.com/en-us/dotnet/standard/threading/barrier

The full documentation on Barrier is here: https://docs.microsoft.com/en-us/dotnet/standard/threading/barrier

简而言之,您可以通过分别调用 AddParticipantRemoveParticipant 随时添加或删除参与者.

In a nutshell, you can add or remove a participant at any time by calling respectively AddParticipant or RemoveParticipant.

这篇关于向已定义的屏障注册新线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆