Multithreading in C# allows you to run multiple threads concurrently, enabling you to make better use of system resources and improve the performance of your applications. Here’s a simple example of how to implement multithreading in C#:
using System;
using System.Threading;
class Program
{
static void Main()
{
Thread thread1 = new Thread(PrintNumbers);
Thread thread2 = new Thread(PrintLetters);
thread1.Start();
thread2.Start();
thread1.Join();
thread2.Join();
Console.WriteLine("Both threads have completed.");
}
static void PrintNumbers()
{
for (int i = 1; i <= 5; i++)
{
Console.WriteLine(i);
Thread.Sleep(1000); // Simulate work
}
}
static void PrintLetters()
{
for (char c = 'A'; c <= 'E'; c++)
{
Console.WriteLine(c);
Thread.Sleep(1000); // Simulate work
}
}
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?