Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Sharing data across threads

Sharing data across threads

Scheduled Pinned Locked Moved .NET (Core and Framework)
csscomlearning
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hendrik Debedts
    wrote on last edited by
    #1

    Consider the following: namespace Lesson2 { class Program { static void Main(string[] args) { System.Threading.ThreadStart oThreadStart; oThreadStart = new System.Threading.ThreadStart(UpdateCount); System.Threading.Thread[] oThreads; oThreads = new System.Threading.Thread[10]; for (int i = 0; i < oThreads.Length; i++) { oThreads[i] = new System.Threading.Thread(oThreadStart); oThreads[i].Start(); } // Wait for them to complete for (int i = 0; i < oThreads.Length; i++) { /* * The different threads in 'oThreads' use the same * variable 'Counter.Count'. * => The variable 'Counter.Count' is shared by these threads. * * Without 'Join' the 'WriteLine' does his work before the * last thread is done. * => Total is less then 100.000. */ oThreads[i].Join(); } /* * Show to the console the total value of 'Counter.Count' *(after the 10 threads) * Should be 10 * 10.000 = 100.000 */ Console.WriteLine("Total: {0}", Counter.Count); Console.ReadLine(); } static void UpdateCount() { for (int i = 1; i <= 10000; ++i) { Counter.Count = Counter.Count + 1; } } } public class Counter { public static int Count; } } The expected 'Total' that is written to the console is '100.000' (10 threads that increment 'Counter.Count' with '10.000'). The book i'm currently reading (http://www.amazon.com/MCTS-Self-Paced-Training-Exam-70-536/dp/0735622779) says that if you run this code on a hyperthreading- or a multiprocessorsystem the 'Total' that is written to the console will sometimes be less than '100.000'. I don't understand the explanation.

    K 1 Reply Last reply
    0
    • H Hendrik Debedts

      Consider the following: namespace Lesson2 { class Program { static void Main(string[] args) { System.Threading.ThreadStart oThreadStart; oThreadStart = new System.Threading.ThreadStart(UpdateCount); System.Threading.Thread[] oThreads; oThreads = new System.Threading.Thread[10]; for (int i = 0; i < oThreads.Length; i++) { oThreads[i] = new System.Threading.Thread(oThreadStart); oThreads[i].Start(); } // Wait for them to complete for (int i = 0; i < oThreads.Length; i++) { /* * The different threads in 'oThreads' use the same * variable 'Counter.Count'. * => The variable 'Counter.Count' is shared by these threads. * * Without 'Join' the 'WriteLine' does his work before the * last thread is done. * => Total is less then 100.000. */ oThreads[i].Join(); } /* * Show to the console the total value of 'Counter.Count' *(after the 10 threads) * Should be 10 * 10.000 = 100.000 */ Console.WriteLine("Total: {0}", Counter.Count); Console.ReadLine(); } static void UpdateCount() { for (int i = 1; i <= 10000; ++i) { Counter.Count = Counter.Count + 1; } } } public class Counter { public static int Count; } } The expected 'Total' that is written to the console is '100.000' (10 threads that increment 'Counter.Count' with '10.000'). The book i'm currently reading (http://www.amazon.com/MCTS-Self-Paced-Training-Exam-70-536/dp/0735622779) says that if you run this code on a hyperthreading- or a multiprocessorsystem the 'Total' that is written to the console will sometimes be less than '100.000'. I don't understand the explanation.

      K Offline
      K Offline
      Keith Malwitz
      wrote on last edited by
      #2

      The potential problem on a hyperthreaded or multi-processor PC is due to the fact that each processor in a PC maintains its own cache. Therefore, if any of the threads executing in your routine happen to run on a different processor, that processor updates its own cache and does not automatically publish the new value to the other processor. This would cause your counter to 'miss' that particular iteration of the counter. There are several potential solutions to get around this, for instance using the VolatileRead and VolatileWrite methods of the Thread class (these use shared memory that all processor's can access, rather than storing the value of your variable in a CPU register) or using the MemoryBarrier of the Thread class (this method flushes the caches and CPU registers of all the processors into memory, ensuring that variables contain current data). You can get plenty of info on the Thread class and it's methods on the MSDN website. Hope that helps.

      H 1 Reply Last reply
      0
      • K Keith Malwitz

        The potential problem on a hyperthreaded or multi-processor PC is due to the fact that each processor in a PC maintains its own cache. Therefore, if any of the threads executing in your routine happen to run on a different processor, that processor updates its own cache and does not automatically publish the new value to the other processor. This would cause your counter to 'miss' that particular iteration of the counter. There are several potential solutions to get around this, for instance using the VolatileRead and VolatileWrite methods of the Thread class (these use shared memory that all processor's can access, rather than storing the value of your variable in a CPU register) or using the MemoryBarrier of the Thread class (this method flushes the caches and CPU registers of all the processors into memory, ensuring that variables contain current data). You can get plenty of info on the Thread class and it's methods on the MSDN website. Hope that helps.

        H Offline
        H Offline
        Hendrik Debedts
        wrote on last edited by
        #3

        Thx

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups