C#应用程序中的多线程 [英] Multithreading in c# application

查看:109
本文介绍了C#应用程序中的多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何编写一个多线程C#代码示例,以用随机数填充int [10000]数组.为当前硬件配置使用最佳线程数.
请给我安慰
谢谢与问候

Arvind Kumar Singh

How we Write an example of multithreaded C# code to fill int[10000] array with random numbers. Use the optimal number of threads for the current hardware configuration.
Please Give Me soluation
Thanks & Regards

Arvind Kumar Singh

推荐答案

请记住,如果将同一数组传递给不同的线程,则核心高速缓存和数据复制可能会出现问题,并且这样的微不足道的操作可能会使线程的最佳数量达到1.

如果您使用的是.Net 4,则应查看Simon提到的任务并行库.如果不是,则应查看ThreadPool.因为这大概是一项家庭作业(对于任何类型的业务场景都是荒谬的要求),告诉您从哪里开始寻找就和我一样明确.
Bear in mind that if you pass the same array around to different threads, you are likely to have issues with the core cache and data copying, and that will probably make the optimal number of threads 1 for such a trivial operation.

You should look at the Task Parallel Library as Simon mentioned if you are using .Net 4; if not, you should look at the ThreadPool. As this is presumably a homework exercise (it is a ridiculous requirement for any sort of business scenario) telling you where to start looking is as explicit as I will be.



如果您可以使用.NET 4.0,则Sacha Barber撰写了一系列有关任务并行库"的文章.这是第一个:
任务并行库:n之1 [
If you can use .NET 4.0 then Sacha Barber has written a series of articles about the Task Parallel Library. Here is the first one:
Task Parallel Library: 1 of n[^]

As for the optimal number of threads, I don''t remember if mr. Barber convers that, but in most cases it would be best to let the ThreadPool handle that. ThreadPool is covered in mr. Barber''s articles.


这篇关于C#应用程序中的多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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