Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Beginner's Guide to Threading in .NET: Part 3 of n

, 10 Aug 2008
This article is all about how to control the synchronization of different threads.
threadingpart3.zip
ThreadingPart3
AutoResetEventTest
bin
Debug
AutoResetEventTest.exe
AutoResetEventTest.vshost.exe
AutoResetEventTest.vshost.exe.manifest
ThreadingPart3.vshost.exe.manifest
obj
Debug
TempPE
Properties
InterlockedTest
bin
Debug
InterlockedTest.exe
InterlockedTest.vshost.exe
InterlockedTest.vshost.exe.manifest
obj
Debug
TempPE
Properties
LockTest
bin
Debug
LockTest.exe
LockTest.vshost.exe
LockTest.vshost.exe.manifest
obj
Debug
TempPE
Properties
ManualResetEventTest
bin
Debug
ManualResetEventTest.exe
ManualResetEventTest.vshost.exe
obj
Debug
TempPE
Properties
MethodImplSynchronizedTest
bin
Debug
MethodImplSynchronizedTest.exe
MethodImplSynchronizedTest.vshost.exe
MethodImplSynchronizedTest.vshost.exe.manifest
obj
Debug
TempPE
Properties
MonitorTest
bin
Debug
MonitorTest.exe
MonitorTest.vshost.exe
MonitorTest.vshost.exe.manifest
obj
Debug
TempPE
Properties
MutexTest
bin
Debug
MutexTest.exe
MutexTest.vshost.exe
MutexTest.vshost.exe.manifest
obj
Debug
TempPE
Properties
NoLockTest
bin
Debug
NoLockTest.exe
NoLockTest.vshost.exe
NoLockTest.vshost.exe.manifest
obj
Debug
TempPE
Properties
OrderSystem
bin
Debug
OrderSystem.exe
OrderSystem.vshost.exe
OrderSystem.vshost.exe.manifest
obj
Debug
TempPE
Properties
ReaderWriterLockSlim
bin
Debug
ReaderWriterLockSlimTest.exe
ReaderWriterLockSlimTest.vshost.exe
ReaderWriterLockSlimTest.vshost.exe.manifest
obj
Debug
TempPE
Properties
SemaphoreTest
bin
Debug
SemaphoreTest.exe
SemaphoreTest.vshost.exe
SemaphoreTest.vshost.exe.manifest
obj
Debug
TempPE
Properties
ThreadingPart3.gpState
using System;
using System.Threading;

namespace LockTest
{

    /// <summary>
    /// This shows how to create a critical section
    /// using the lock keyword
    /// </summary>
    class Program
    {
        static object syncLock = new object();
        static int item1 = 54, item2 = 21;
        public static Thread T1;
        public static Thread T2;

        static void Main(string[] args)
        {
            T1 = new Thread((ThreadStart)delegate
            {
                DoCalc();
            });
            T2 = new Thread((ThreadStart)delegate
            {
                DoCalc();
            });
            T1.Name = "T1";
            T2.Name = "T2";
            T1.Start();
            T2.Start();
            Console.ReadLine();
        }

        private static void DoCalc()
        {
            lock (syncLock)
            {
                item2 = 10;
                if (item1 != 0)
                    Console.WriteLine(item1 / item2);
                item2 = 0;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

You may also be interested in...

About the Author

Sacha Barber
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)
 
- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence
 
Both of these at Sussex University UK.
 
Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 10 Aug 2008
Article Copyright 2008 by Sacha Barber
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid