Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

C#
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)
Posted
Updated 18-Nov-12 19:45pm
v3
Comments
Matt T Heffron 18-Nov-12 19:01pm    
This code should fail (throwing exceptions) in both cases because you are modifying the collections that you are iterating over with the foreach(...). This is not allowed.

1 solution

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:
C#
lock(someLockObject)
    firstsortedlist.Add(item.Key, item.Value);
// where someLockObject is some really existing object (not a null reference) shared by all tasks


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
 
Share this answer
 
v2
Comments
Member 9574034 19-Nov-12 2:19am    
Thank you for complete illustration.
Sergey Alexandrovich Kryukov 19-Nov-12 11:45am    
You are welcome.
Than please consider accepting the answer formally (green button) -- thanks.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900