Skip to content

Latest commit

 

History

History
117 lines (92 loc) · 4.19 KB

Multithreading Programming.md

File metadata and controls

117 lines (92 loc) · 4.19 KB

MultiThreading Programming

Simple Explanation

A thread is a lightweight execution unit that is used to execute code concurrently. It allows multiple parts of a program to run at the same time. Threads are useful for performing time-consuming tasks without blocking the main thread of execution. In C# and .NET, you can create and manage threads using the System.Threading namespace.

Deep Explanation

A thread is a basic unit of CPU utilization that is scheduled by the operating system. Each thread represents an independent flow of execution that can run concurrently with other threads. In a multi-threaded application, multiple threads can run concurrently and perform different tasks at the same time. Threads are commonly used to perform time-consuming operations in the background, such as I/O operations, network communications, or computational tasks.

In C# and .NET, threads are managed by the System.Threading namespace. You can create a new thread by instantiating the Thread class and passing a delegate that represents the code to be executed in the new thread. For example:

using System;
using System.Threading;

public class Program
{
    static void Main(string[] args)
    {
        Thread thread = new Thread(DoWork);
        thread.Start();
        Console.WriteLine("Main thread is running...");
    }

    static void DoWork()
    {
        Console.WriteLine("Worker thread is running...");
    }
}

This code creates a new thread and starts executing the DoWork method in the new thread. The Main method continues executing in the original thread.

In addition to creating threads, you can also control their execution using various methods provided by the Thread class. For example, you can pause a thread using the Thread.Sleep method, or wait for a thread to finish using the Thread.Join method. You can also control the priority of a thread using the Thread.Priority property.

However, multi-threaded programming can be challenging because multiple threads can access shared resources concurrently, leading to synchronization issues and race conditions. To avoid these issues, you can use various synchronization mechanisms provided by the System.Threading namespace, such as locks, semaphores, and mutexes, to control access to shared resources.

Examples

Here is an example that demonstrates how to use threads to perform a time-consuming operation in the background:

using System;
using System.Threading;

public class Program
{
    static void Main(string[] args)
    {
        Thread thread = new Thread(new ThreadStart(DoWork));
        thread.Start();
        Console.WriteLine("Main thread is running...");
        // Wait for the worker thread to finish
        thread.Join();
        Console.WriteLine("Worker thread has finished.");
    }

    static void DoWork()
    {
        Console.WriteLine("Worker thread is running...");
        // Simulate a time-consuming operation
        Thread.Sleep(5000);
        Console.WriteLine("Worker thread has finished.");
    }
}

Here is an example that demonstrates how to use locks to synchronize access to a shared resource:

using System;
using System.Threading;

class Program
{
    static void Main()
    {
        Thread thread1 = new Thread(PrintNumbers);
        thread1.Start();

        for (int i = 0; i < 5; i++)
        {
            Console.WriteLine("Main Thread: " + i);
            Thread.Sleep(1000);
        }

        Console.ReadLine();
    }

    static void PrintNumbers()
    {
        for (int i = 0; i < 5; i++)
        {
            Console.WriteLine("Secondary Thread: " + i);
            Thread.Sleep(1000);
        }
    }
}

This example creates a new thread and starts it with the PrintNumbers method. The Main method also contains a loop that outputs a message to the console every second. After starting the new thread, the Main method will continue executing its loop and printing its messages to the console. At the same time, the new thread will execute its loop in the PrintNumbers method and output its messages to the console.

Output:

Main Thread: 0
Secondary Thread: 0
Main Thread: 1
Secondary Thread: 1
Main Thread: 2
Secondary Thread: 2
Main Thread: 3
Secondary Thread: 3
Main Thread: 4
Secondary Thread: 4