并行任务和排序列表线程安全 [英] Parallel task and sorted list thread safety

查看:99
本文介绍了并行任务和排序列表线程安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据代码段,线程安全吗?如果没有,我该怎么办?

其次,CPU内核数与任务数之间是否有关系?

According to code segment, is it thread safe? if not what should I do?

Second, Is there any relation between number of cores of CPU and number of tasks?

var t = new Task[2]
{
    Task.Factory.StartNew(()=>
    {
        foreach (var item in firstsortedlist)
        {
            firstlist.Add(item.Key, item.Value);
    }
    }),
    Task.Factory.StartNew(()=>
    {
        foreach (var item in sortedlist2)
        {
            list2.Add(item.Key, item.Value);
        }
    })
};
Task.WaitAll(t)

推荐答案

您的集合类型应该是线程安全的.不是,这不是问题,但是您应该使使用线程的所有调用变得安全,只需将每个调用包装在同一锁定对象的lock语句中即可:
Your collection type should be thread-safe. It is not, this is not a problem, but you should make all calls you use thread safe, by simply wrapping each of them in the lock statement on the same lock object:
lock(someLockObject)
    firstsortedlist.Add(item.Key, item.Value);
// where someLockObject is some really existing object (not a null reference) shared by all tasks



代码数量和任务数量之间没有直接关系,但是,如果内核数量大于或接近任务数量,则可以通过添加任务来有效地提高性能,因为它可以提高进程的CPU利用率,但是仅如果他们是独立的,不互相等待;随着任务数量的增加,您并不能真正提高吞吐量.例如,如果您只有一个CPU带有一个代码,并且就问题的逻辑而言,任务a是否在异步状态下一个接一个地执行就没关系,并行执行实际上会降低性能,因为并行机制本身有一些开销浪费CPU时间.

您不能将这些CPU利用率考虑因素用于问题本质上属于并行性的进程.例如,如果您有任何非循环任务要在UI后台执行,则UI始终需要线程化,因为UI线程仅在用户发送某些输入时占用CPU时间,并且在浪费零CPU时间之间进行睡眠.在这种情况下,抢占式多任务处理使进程可以执行一些后台任务,而不会显着降低UI速度.如果您通过网络进行常规且费时的通信,硬件控制,数据获取,任何耗时的计算等等,就会发生类似的事情-在所有此类情况下,即使只有少量CPU内核,多线程也可以为您带来不可或缺的好处,甚至一个.

—SA



There is no direct relationships between number of codes and number of tasks, but if number of cores is greater or close to the number of tasks, you can effectively improve performance by adding tasks, because it improves CPU utilization level by your process, but only if they are independent and don''t wait for each others; as number of tasks gets greater, you don''t really improve throughput. For example, if you have just one CPU with one code, and, for the logic of the problem, it does not matter if the tasks a are executed one after another on asynchronously, parallel execution actually decrease performance, because the mechanism of parallelism itself has some overhead wasting CPU time.

You cannot apply these CPU utilization consideration for the processes where parallelism lies in the nature of the problem. For example, UI always needs threading if you have any non-circular task to execute in the UI background, because the UI thread only takes CPU time when a user sends some input and sleeps in between wasting zero CPU time. In this case, preemptive multitasking allows the process to execute some background tasks without considerable slowing down of the UI. Similar things happens if you have regular and time-consuming communication over network, hardware control, data acquisition, any time-consuming calculations, and a lot more — in all such scenarios multithreading gives you indispensable benefits even with small numbers of CPU cores, even with one.

—SA


这篇关于并行任务和排序列表线程安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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