Mri*_*boj 1 .net c# parallel-processing multithreading
As I understand Parallel API use the Thread pool internally and they queue up the items for parallel processing, however, when I checked up the execution of one such parallel loop using SOS debugger, then my understanding is that if I have 10 tasks lined up then all of them might not go in parallel and CLR would decide how many threads to dispatch for the given tasks to be executed, so it may be 4 or 5 or 6 (varied number in each execution)
但是,如果我的总任务数如果不是很高,如10并且我希望所有这些都并行,因为所有这些都是长时间运行的,那么最好将它们放在传统线程上,这将确保每个任务1个线程它们都是并行的如果任务的数量是好的数字,如100,那么使用Parallel或Threadpool是一个实用的解决方案,因为我们不希望每个进程调用100个单独的线程
请分享您的观点,我理解Parallel API的好处,使完整的并行编程非常容易实现,但在这里我的目标是不同的
默认情况下,.NET线程池初始化许多工作线程,这些线程对应于计算机上的逻辑核心数.它随后采用爬山式启发式算法,根据当前任务工作负载调整此数量,当任务需要很长时间才能完成时启动新的工作线程.
您希望通过线程超额预订(即每个逻辑核心运行多个线程)同时执行长期运行的任务,这是正确的.实际上,任务并行库基础结构(TPL)通过该LongRunning选项专门为此场景提供,该选项(在当前实现下)为每个标记的任务生成新的专用线程.
Task.Factory.StartNew(myLongRunningAction, TaskCreationOptions.LongRunning);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
773 次 |
| 最近记录: |