Click here to Skip to main content
13,139,254 members (67,809 online)
Rate this:
Please Sign up or sign in to vote.
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?

var t = new Task[2]
        foreach (var item in firstsortedlist)
            firstlist.Add(item.Key, item.Value);
        foreach (var item in sortedlist2)
            list2.Add(item.Key, item.Value);
Posted 18-Nov-12 11:18am
Updated 18-Nov-12 19:45pm
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

Rate this: bad
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:
    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.

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170915.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2017
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