Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem with Mutex in multithreading Application

Problem with Mutex in multithreading Application

Scheduled Pinned Locked Moved C#
helptutorialannouncementlearning
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alexandr Sergeevich Ilyin
    wrote on last edited by
    #1

    I have application, that creates 2 threads, Both threads use the same resources and I want, they to be synchronised using Mutex. I am using code like this public class Test { protected Mutex mutex; public Test() { mutex = new Mutex(false); Thread A = new Thread(new ThreadStart(TF1)); Thread B = new Thread(new ThreadStart(TF2)); A.Start(); B.Start(); } void TF1() { while(true) { mutex.WaitOne(); Console.Write("do some work 1"); mutex.Release(); Thread.Sleep(100); } } void TF2() { while(true) { mutex.WaitOne(); Console.Write("do some work 2"); mutex.Release(); Thread.Sleep(100); } } } -------------------------------------------- It works fine some time, but one moment both Threads are waiting for something, for example Console Input or Window Resize. And I do not understand why. (Of course, I am not using "Console.Read" function in a Thread functions. )

    W S M 3 Replies Last reply
    0
    • A Alexandr Sergeevich Ilyin

      I have application, that creates 2 threads, Both threads use the same resources and I want, they to be synchronised using Mutex. I am using code like this public class Test { protected Mutex mutex; public Test() { mutex = new Mutex(false); Thread A = new Thread(new ThreadStart(TF1)); Thread B = new Thread(new ThreadStart(TF2)); A.Start(); B.Start(); } void TF1() { while(true) { mutex.WaitOne(); Console.Write("do some work 1"); mutex.Release(); Thread.Sleep(100); } } void TF2() { while(true) { mutex.WaitOne(); Console.Write("do some work 2"); mutex.Release(); Thread.Sleep(100); } } } -------------------------------------------- It works fine some time, but one moment both Threads are waiting for something, for example Console Input or Window Resize. And I do not understand why. (Of course, I am not using "Console.Read" function in a Thread functions. )

      W Offline
      W Offline
      whizzs
      wrote on last edited by
      #2

      Try switching your Thread.Sleep(100) with your mutex.Release(). Since Thread.Sleep() is a static method you can't be sure which thread is sleeping if you have released the synchronization Also, I don't think your implementation is correct. Since your threads do not own the Mutex I do not believe they will be synchronized. You should have both threads call the same function so the Mutex can protect the resource.

      public class Test
      {
      protected Mutex mutex;
      public Test()
      {
      mutex = new Mutex(false);
      Thread A = new Thread(new ThreadStart(TF));
      Thread B = new Thread(new ThreadStart(TF));
      A.Start();
      B.Start();
      }
      void TF()
      {
      while(true)
      {
      mutex.WaitOne();
      Console.Write("do some work {0}", Thread.CurrentThread.Name);
      Thread.Sleep(100);
      mutex.ReleaseMutex();
      }
      }
      }

      and since your synchronization is inside an infinite while loop I don't know what will happen as far as sharing process time -- modified at 16:11 Tuesday 22nd November, 2005

      A S 2 Replies Last reply
      0
      • W whizzs

        Try switching your Thread.Sleep(100) with your mutex.Release(). Since Thread.Sleep() is a static method you can't be sure which thread is sleeping if you have released the synchronization Also, I don't think your implementation is correct. Since your threads do not own the Mutex I do not believe they will be synchronized. You should have both threads call the same function so the Mutex can protect the resource.

        public class Test
        {
        protected Mutex mutex;
        public Test()
        {
        mutex = new Mutex(false);
        Thread A = new Thread(new ThreadStart(TF));
        Thread B = new Thread(new ThreadStart(TF));
        A.Start();
        B.Start();
        }
        void TF()
        {
        while(true)
        {
        mutex.WaitOne();
        Console.Write("do some work {0}", Thread.CurrentThread.Name);
        Thread.Sleep(100);
        mutex.ReleaseMutex();
        }
        }
        }

        and since your synchronization is inside an infinite while loop I don't know what will happen as far as sharing process time -- modified at 16:11 Tuesday 22nd November, 2005

        A Offline
        A Offline
        Alexandr Sergeevich Ilyin
        wrote on last edited by
        #3

        Thank you! I'll try to make it static now. BIG Thanks!

        1 Reply Last reply
        0
        • W whizzs

          Try switching your Thread.Sleep(100) with your mutex.Release(). Since Thread.Sleep() is a static method you can't be sure which thread is sleeping if you have released the synchronization Also, I don't think your implementation is correct. Since your threads do not own the Mutex I do not believe they will be synchronized. You should have both threads call the same function so the Mutex can protect the resource.

          public class Test
          {
          protected Mutex mutex;
          public Test()
          {
          mutex = new Mutex(false);
          Thread A = new Thread(new ThreadStart(TF));
          Thread B = new Thread(new ThreadStart(TF));
          A.Start();
          B.Start();
          }
          void TF()
          {
          while(true)
          {
          mutex.WaitOne();
          Console.Write("do some work {0}", Thread.CurrentThread.Name);
          Thread.Sleep(100);
          mutex.ReleaseMutex();
          }
          }
          }

          and since your synchronization is inside an infinite while loop I don't know what will happen as far as sharing process time -- modified at 16:11 Tuesday 22nd November, 2005

          S Offline
          S Offline
          S Senthil Kumar
          wrote on last edited by
          #4

          whizzs wrote:

          Also, I don't think your implementation is correct. Since your threads do not own the Mutex I do not believe they will be synchronized. You should have both threads call the same function so the Mutex can protect the resource

          Huh? A threads owns the mutex once the WaitOne function completes execution. And it's absolute *** that the code will work only if the mutex is within one function. The only requirement is that the Mutex object must be shared between the two threads, which the OP code does. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

          1 Reply Last reply
          0
          • A Alexandr Sergeevich Ilyin

            I have application, that creates 2 threads, Both threads use the same resources and I want, they to be synchronised using Mutex. I am using code like this public class Test { protected Mutex mutex; public Test() { mutex = new Mutex(false); Thread A = new Thread(new ThreadStart(TF1)); Thread B = new Thread(new ThreadStart(TF2)); A.Start(); B.Start(); } void TF1() { while(true) { mutex.WaitOne(); Console.Write("do some work 1"); mutex.Release(); Thread.Sleep(100); } } void TF2() { while(true) { mutex.WaitOne(); Console.Write("do some work 2"); mutex.Release(); Thread.Sleep(100); } } } -------------------------------------------- It works fine some time, but one moment both Threads are waiting for something, for example Console Input or Window Resize. And I do not understand why. (Of course, I am not using "Console.Read" function in a Thread functions. )

            S Offline
            S Offline
            S Senthil Kumar
            wrote on last edited by
            #5

            Alexandr Sergeevich Ilyin wrote:

            but one moment both Threads are waiting for something, for example Console Input or Window Resize.

            Could you be a bit more clear? Do you notice just a pause or do the two threads deadlock? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

            A 1 Reply Last reply
            0
            • S S Senthil Kumar

              Alexandr Sergeevich Ilyin wrote:

              but one moment both Threads are waiting for something, for example Console Input or Window Resize.

              Could you be a bit more clear? Do you notice just a pause or do the two threads deadlock? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

              A Offline
              A Offline
              Alexandr Sergeevich Ilyin
              wrote on last edited by
              #6

              It is not deadlock, Just pause. It is enough to run some application or resize window to continue... -------------------------------------------------------------------- In .NET examples Mutex variable is always static, is it Important ? Why ?

              1 Reply Last reply
              0
              • A Alexandr Sergeevich Ilyin

                I have application, that creates 2 threads, Both threads use the same resources and I want, they to be synchronised using Mutex. I am using code like this public class Test { protected Mutex mutex; public Test() { mutex = new Mutex(false); Thread A = new Thread(new ThreadStart(TF1)); Thread B = new Thread(new ThreadStart(TF2)); A.Start(); B.Start(); } void TF1() { while(true) { mutex.WaitOne(); Console.Write("do some work 1"); mutex.Release(); Thread.Sleep(100); } } void TF2() { while(true) { mutex.WaitOne(); Console.Write("do some work 2"); mutex.Release(); Thread.Sleep(100); } } } -------------------------------------------- It works fine some time, but one moment both Threads are waiting for something, for example Console Input or Window Resize. And I do not understand why. (Of course, I am not using "Console.Read" function in a Thread functions. )

                M Offline
                M Offline
                mcljava
                wrote on last edited by
                #7

                Take a look at AutoResetEvent or ManualResetEvent as an alternative to using Mutex. Also looking at your code both your threads go into indefinite wait. one solution is: in TF1 delete the mutex.WaitOne statement. Then replace delete the Thread.Sleep(100) line. As the last statement in TF1, add mutex.WaitOne(). This will execute TF1 first, who writes the message, releases the Mutex and then goes into a Wait for state until signaled by TF2. In this way your threads will take turns. If you need both threads to operate concurrently then you need to add the mutex coordination to the mainline code that started the threads. BTW, you dont need the Thread.Sleep() in TF2 either unless you are just trying to slow down the I/O. Mike

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups