Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Clean Code in C#

You're reading from   Clean Code in C# Refactor your legacy C# code base and improve application performance by applying best practices

Arrow left icon
Product type Paperback
Published in Jul 2020
Publisher Packt
ISBN-13 9781838982973
Length 500 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Jason Alls Jason Alls
Author Profile Icon Jason Alls
Jason Alls
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Coding Standards and Principles in C# 2. Code Review – Process and Importance FREE CHAPTER 3. Classes, Objects, and Data Structures 4. Writing Clean Functions 5. Exception Handling 6. Unit Testing 7. End-to-End System Testing 8. Threading and Concurrency 9. Designing and Developing APIs 10. Securing APIs with API Keys and Azure Key Vault 11. Addressing Cross-Cutting Concerns 12. Using Tools to Improve Code Quality 13. Refactoring C# Code – Identifying Code Smells 14. Refactoring C# Code – Implementing Design Patterns 15. Assessments 16. Other Books You May Enjoy

Preventing race conditions

When multiple threads using the same resource produce different outcomes due to the timings of each thread, this is known as a race condition. We will demonstrate this in action now.

In our demonstration, we will have two threads. Each thread will call a method to print the alphabet. One method will print the alphabet using uppercase letters. The second method will print the alphabet using lowercase letters. From the demonstration, we'll see how the output is wrong, and every time the program is run, the output will be wrong.

First, add the ThreadingRaceCondition() method:

static void ThreadingRaceCondition()
{
Thread T1 = new Thread(Method1);
T1.Start();
Thread T2 = new Thread(Method2);
T2.Start();
}

ThreadingRaceCondition() produces two threads and starts them. It also references two methods. Method1() prints out the alphabet in uppercase and Method2() prints out the alphabet...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime