ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCSI-415Threads And Synchroniz…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    CSI-415

    Advanced Programming

    Progress0 / 55 done
    Topics55
    1Visual Programming Basics2Introduction to Events3Fundamentals of Event-Driven Programming4Message Handling5User Interfaces6Graphics Device Interface7Painting and Drawing8Windows Management9Input Devices10Resources11String and Menu Resource12Dialogs and Windows Controls13Common Controls14Dynamic Link Libraries (DLLs)15Threads and Synchronization16Network Programming17Building Class Libraries at the Command Line18Class Libraries19Using References20Assemblies21Private Assembly Deployment22Shared Assembly Deployment23Configuration Overview24Configuration Files25Programmatic Access to Configuration26Using SDK Tools for Signing and Deployment27Metadata28Reflection29Late Binding30Directories and Files31Serialization32Attributes33Memory Management and Garbage Collection34Threading and Synchronization35Asynchronous Delegates36Application Domains37Marshal by Value38Marshal by Reference39Authentication and Authorization40Configuring Security41Code Access Security42Code Groups43Evidence44Permissions45Role-Based Security46Principals and Identities47Using Data Readers48Using Data Sets49Interacting with XML Data50Tracing Event Logs51Using the Boolean Switch and Trace Switch Classes52Print Debugging Information with the Debug Class53Instrumenting Release Builds with the Trace Class54Using Listeners55Implementing Custom Listeners
    7 min read
    Topic 15 of 55
    Topic Study Note Illustration
    15

    Threads and Synchronization

    In C#, threads and synchronization are crucial concepts when dealing with concurrent execution. Multithreading allows an application to perform multiple operations simultaneously, improving performance and responsiveness. Synchronization...

    Threads and Synchronization in C#

    In C#, threads and synchronization are crucial concepts when dealing with concurrent execution. Multithreading allows an application to perform multiple operations simultaneously, improving performance and responsiveness. Synchronization ensures that shared resources are accessed in a thread-safe manner, preventing issues like data corruption, race conditions, and deadlocks.

    Below is a detailed explanation of threads and synchronization in C#.


    1. Threads in C#

    A thread is the smallest unit of execution within a process. It represents a single path of execution in a program. In modern applications, multiple threads can run concurrently, enabling multitasking and more efficient use of CPU resources.

    Creating and Starting Threads

    In C#, threads are created and managed using the System.Threading namespace. The Thread class allows you to create and manipulate threads.

    Basic Thread Creation and Execution

    using System;
    using System.Threading;
    
    class Program
    {
        static void Main()
        {
            // Create a new thread and pass the method to be executed
            Thread thread = new Thread(DoWork);
            
            // Start the thread
            thread.Start();
    
            // Wait for the thread to finish
            thread.Join();
        }
    
        // Method to be executed by the thread
        static void DoWork()
        {
            Console.WriteLine("Thread is working...");
            Thread.Sleep(2000); // Simulate work by sleeping for 2 seconds
            Console.WriteLine("Thread work is done!");
        }
    }
    
    • Thread.Start(): Starts the execution of the thread.
    • Thread.Join(): Makes the main thread wait for the other thread to finish.

    Thread Pool

    A ThreadPool provides a collection of worker threads that can be used for short-lived tasks. Instead of creating a new thread each time, you can use the thread pool to reuse existing threads.

    using System;
    using System.Threading;
    
    class Program
    {
        static void Main()
        {
            // Queue a work item to the thread pool
            ThreadPool.QueueUserWorkItem(DoWork);
        }
    
        static void DoWork(object state)
        {
            Console.WriteLine("Thread pool worker is working...");
        }
    }
    

    The ThreadPool is more efficient when managing many short-lived tasks compared to creating individual threads, as it avoids the overhead of creating and destroying threads.


    2. Synchronization in C#

    When multiple threads access shared resources simultaneously, it can lead to problems like race conditions, data corruption, and unpredictable behavior. Synchronization is used to ensure that only one thread accesses shared resources at a time, preventing these issues.

    Thread Synchronization Techniques

    1. Locking with lock Statement

    The lock keyword in C# is used to ensure that only one thread can execute a block of code at a time. It works by obtaining a mutex (mutual exclusion) on an object.

    using System;
    using System.Threading;
    
    class Program
    {
        private static int sharedResource = 0;
        private static object lockObject = new object();
    
        static void Main()
        {
            Thread thread1 = new Thread(Increment);
            Thread thread2 = new Thread(Increment);
    
            thread1.Start();
            thread2.Start();
    
            thread1.Join();
            thread2.Join();
    
            Console.WriteLine($"Shared resource value: {sharedResource}");
        }
    
        static void Increment()
        {
            lock (lockObject) // Only one thread can enter this block at a time
            {
                int temp = sharedResource;
                temp++;
                Thread.Sleep(100); // Simulate some work
                sharedResource = temp;
            }
        }
    }
    
    • The lock keyword ensures that the code inside the block is only executed by one thread at a time.
    • The lockObject is an object that is used to synchronize access. You can use any object, but it is common to use a dedicated locking object.

    2. Monitor Class

    The Monitor class provides more fine-grained control over locking and synchronization. It allows for acquiring and releasing locks, as well as waiting for conditions.

    using System;
    using System.Threading;
    
    class Program
    {
        private static int sharedResource = 0;
        private static object lockObject = new object();
    
        static void Main()
        {
            Thread thread1 = new Thread(Increment);
            Thread thread2 = new Thread(Increment);
    
            thread1.Start();
            thread2.Start();
    
            thread1.Join();
            thread2.Join();
    
            Console.WriteLine($"Shared resource value: {sharedResource}");
        }
    
        static void Increment()
        {
            Monitor.Enter(lockObject); // Acquire lock
    
            try
            {
                int temp = sharedResource;
                temp++;
                Thread.Sleep(100); // Simulate some work
                sharedResource = temp;
            }
            finally
            {
                Monitor.Exit(lockObject); // Release lock
            }
        }
    }
    
    • Monitor.Enter(lockObject): Acquires the lock.
    • Monitor.Exit(lockObject): Releases the lock.
    • The try-finally block ensures that the lock is always released, even if an exception occurs.

    3. Mutex Class

    Semaphore (programming)
    Visual ReferenceSemaphore (Programming)
    Wikipedia

    A Mutex is used to synchronize threads across processes. It can be used to control access to a resource across different applications, not just within the same process.

    using System;
    using System.Threading;
    
    class Program
    {
        static Mutex mutex = new Mutex();
    
        static void Main()
        {
            Thread thread1 = new Thread(DoWork);
            Thread thread2 = new Thread(DoWork);
    
            thread1.Start();
            thread2.Start();
    
            thread1.Join();
            thread2.Join();
        }
    
        static void DoWork()
        {
            mutex.WaitOne(); // Acquire the mutex
            try
            {
                Console.WriteLine("Working...");
                Thread.Sleep(1000); // Simulate work
            }
            finally
            {
                mutex.ReleaseMutex(); // Release the mutex
            }
        }
    }
    
    • mutex.WaitOne(): Acquires the mutex lock.
    • mutex.ReleaseMutex(): Releases the mutex lock.

    A Mutex is useful when you need synchronization not only within your application but also across different applications or processes.

    4. Semaphore Class

    A Semaphore is used to limit the number of threads that can access a resource concurrently. It allows for more flexibility than a Mutex or lock.

    using System;
    using System.Threading;
    
    class Program
    {
        static Semaphore semaphore = new Semaphore(2, 2); // Allow only 2 threads at a time
    
        static void Main()
        {
            Thread thread1 = new Thread(DoWork);
            Thread thread2 = new Thread(DoWork);
            Thread thread3 = new Thread(DoWork);
    
            thread1.Start();
            thread2.Start();
            thread3.Start();
    
            thread1.Join();
            thread2.Join();
            thread3.Join();
        }
    
        static void DoWork()
        {
            semaphore.WaitOne(); // Acquire the semaphore
            try
            {
                Console.WriteLine("Working...");
                Thread.Sleep(1000); // Simulate work
            }
            finally
            {
                semaphore.Release(); // Release the semaphore
            }
        }
    }
    
    • semaphore.WaitOne(): Acquires the semaphore.
    • semaphore.Release(): Releases the semaphore.

    This allows a maximum of 2 threads to run concurrently in this example, which helps limit the load on a shared resource.


    3. Deadlocks and Starvation

    Deadlocks

    A deadlock occurs when two or more threads are waiting for each other to release resources, causing all the threads involved to be stuck indefinitely. Deadlocks usually happen when two threads acquire multiple locks in a different order.

    Example of Deadlock:

    Deadlock (computer science)
    Visual ReferenceDeadlock (Computer Science)
    Wikipedia
    using System;
    using System.Threading;
    
    class Program
    {
        static object lock1 = new object();
        static object lock2 = new object();
    
        static void Main()
        {
            Thread thread1 = new Thread(DoWork1);
            Thread thread2 = new Thread(DoWork2);
    
            thread1.Start();
            thread2.Start();
    
            thread1.Join();
            thread2.Join();
        }
    
        static void DoWork1()
        {
            lock (lock1)
            {
                Console.WriteLine("Thread 1 acquired lock 1");
                Thread.Sleep(100); // Simulate some work
                lock (lock2)
                {
                    Console.WriteLine("Thread 1 acquired lock 2");
                }
            }
        }
    
        static void DoWork2()
        {
            lock (lock2)
            {
                Console.WriteLine("Thread 2 acquired lock 2");
                Thread.Sleep(100); // Simulate some work
                lock (lock1)
                {
                    Console.WriteLine("Thread 2 acquired lock 1");
                }
            }
        }
    }
    
    • Deadlock Prevention: Always acquire locks in the same order in all threads to avoid circular dependencies.

    Starvation

    Starvation occurs when a thread is perpetually denied access to the resource it needs because other threads are always acquiring it first. It can be avoided by using proper thread scheduling and prioritization.


    4. Conclusion

    • Threads are fundamental to building responsive and efficient applications in C#. They allow for parallel execution and can significantly improve performance.
    • Synchronization ensures safe access to shared resources, preventing issues such as race conditions and data corruption.
    • C# provides several synchronization techniques, including lock, Monitor, Mutex, and Semaphore.

    By understanding how to work with

    ← Previous topic 14
    Dynamic Link Libraries (DLLs)
    Next topic 16 →
    Network Programming

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    7mremaining
    1.2kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Advanced Programming