Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Parallel Task
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)
Posted 18-Nov-12 12:18pm
Edited 18-Nov-12 20:45pm
v3
Comments
Matt T Heffron at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
 
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
  Permalink  
v2
Comments
Member 9574034 at 19-Nov-12 2:19am
   
Thank you for complete illustration.
Sergey Alexandrovich Kryukov at 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)

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 Zoltán Zörgő 99
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100