1

When you schedule a single thread across different processors, does the cache have to be cleared each time? If the cache is not cleared, can't the following happen :

Suppose you have the following (pseudo) code executed by 2 processors, P1 and P2.

     1. foo() {
     2. int x=5;
     3. x=10;
     4. print x;
     5. }

Initially the thread is scheduled on P1, which executes lines 1 and 2, and stores 5 in its cache for x's memory location (on the stack).

The thread is then scheduled on P2 which executes line 3, and stores 10 in its cache for x's memory location.

Finally, the thread is again scheduled on P1, and executing line 4, prints 5 (x's value in P1's cache).

Yet we clearly expect 10 to be printed.

1 Answers1

1

Yes, the processor caches/registers would need to be updated, or at least flushed to main memory, so the thread could get at its recent memory if it migrated to another core/processor.

Luckily the common server/desktop platforms we usually write code for are cache coherent, so the hardware takes care of all the magic to make this work right, and an operating system would anyway need to ensure that the proper memory given to a thread/process are available wherever the operating system decides to run that thread. This is not a task that application code should need to worry about.

In the case you're on a platform that's not cache-coherent, you would probably know - and most likely the OS/thread library you use would take care of making sure the caches are properly updated if the OS decides to migrate a thread to another processor.

nos
  • 223,662
  • 58
  • 417
  • 506