🔬 Lab 10 Multithreading#

📌 Objectives#

  • Students should be able to flash the RGB LED using a background thread.

  • Students should be able to use Timers to implement multithreading.

  • Students should be able to implement an atomic operation to avoid causal dependency.

Note

Testing leads to failure, and failure leads to understanding. -Burt Rutan

📜 Synopsis#

The primary objective of this lab is to develop a program that utilizes Timer interrupts to flash RGB LED. When the red and blue LEDs flash slowly, we can observe the two distinct colors alternately. However, if they flash very fast, we will see only one color. The resulting color will be magenta if there is an equal amount of red and blue (50% each) and violet if there is 33.3% red and 66.6% blue. We can use a foreground thread with a delay function to flash red and blue. In this case, the CPU performs no useful operation but wastes CPU power to run a simple loop. Alternatively, we can use a background thread that utilizes a Timer module. In this case, the LED changes its color in the background while the CPU can perform many other operations.

In the next step, you will investigate causal dependency (or race condition) that causes unexpected results when two or more threads access the same data or memory. For example, incrementing a value with a finite number of iterations concurrently can yield random results.

💻 Procedure#

Setup#

  • Open Code Composer Studio (CCS) and select your workspace.

  • Ensure your Project Explorer is open on the left of the CCS screen. Otherwise, select View > Project Explorer.

  • Open the Lab10_Multithreading project by double-clicking it.

  • Open Code Composer Studio (CCS) and select your workspace.

  • Ensure your Project Explorer is open on the left of the CCS screen.

Examine causal dependency and fix it using an atomic operation#

  • Examining the Foreground Thread

    • Start by thoroughly reviewing the incrementer.asm code.

    • Ensure that the Enable_Interrupt() function inside Program10_3 is commented out. Uncomment the line count = Increment(); inside the for-loop to run the Increment function exclusively in the foreground.

    • Run Program10_3 and take note of the count value displayed on the LCD.

    • Does the displayed value match your expectations? If not, explain.

  • Examining the Background Thread

    • Ensure that the Enable_Interrupt() function inside Program10_3 is uncommented, and the line count = Increment(); inside the for-loop is commented out to execute the Increment function exclusively in the background.

    • Keep in mind that the background thread operates through TimerA2, and the Increment function is invoked every 1 ms.

    • Run Program10_3 and record the count value displayed on the LCD.

    • Does the displayed value align with your expectations? If not, provide an explanation on Gradescope.

  • Examining Multithreading

    • Make sure both Enable_Interrupt() and count = Increment(); are uncommented to enable concurrent execution of the Increment function in both foreground and background threads.

    • Run Program10_3 and document the count value displayed on the LCD.

    • Does the displayed value match your expectations? If not, explain.

  • Fixing Race Condition

    • Make adjustments to Program10_3 to resolve the issue encountered in the previous step, which was attributed to a race condition.

    • Execute Program10_3 once more and take note of the count value displayed on the LCD.

    • Does the displayed value now align with your expectations? Explain how your modifications resolved the issue.

    • Demonstrate Program10_3 displaying the correct value on the LCD while utilizing both foreground and background threads concurrently.

🚚 Deliverables#

Deliverable 1#

  • [5 Points] Demo Program10_2() showing that the RGB LED lights magenta using a background thread.

Deliverable 2#

  • [5 Points] Demo Program10_3() displaying the correct count value on the LCD. You must use both foreground and background threads concurrently.

Deliverable 3#

  • [9.5 Points] Push your code to your repository using git. Write comments in your code.