locked
Thread.Suspend() & Thread.Resume() vs. Mutex.WaitOne() and Mutex.RelaseMutex() RRS feed

  • Question

  • The following code has been depricated

    http://msdn.microsoft.com/en-us/library/system.threading.thread.suspend.aspx

    Imports System.Threading
    
    Module ThreadingModule
      Dim myThread As Thread
    
      Sub Main()
        myThread = New Thread(AddressOf Running)
    
        myThread.Name = "The Sleeping Thread"
        myThread.Priority = ThreadPriority.BelowNormal
    
        myThread.Start()
        Thread.Sleep(1000)
        myThread.Suspend()
        myThread.Resume()
        myThread.Join()
    
        Console.WriteLine(myThread.Name & " has been suspended!")
      End Sub
    
      Public Sub Running()
        Console.WriteLine("Thread is about to sleep!")
        Thread.Sleep(10000)
        Console.WriteLine("Thread is done sleeping!")
      End Sub
    End Module
    This is one possible alternative based on the following example

    http://msdn.microsoft.com/en-us/library/system.threading.mutex.aspx

     

    Imports System.Threading
    
    Module ThreadMutexModule
      Dim myThread As Thread
      Dim mut As New Mutex()
    
      <MTAThread()>
      Sub Main()
        myThread = New Thread(AddressOf Running)
    
        myThread.Name = "The Sleeping Thread"
        myThread.Priority = ThreadPriority.BelowNormal
    
        myThread.Start()
        Thread.Sleep(1000)
    
        myThread.Join()
    
        Console.WriteLine(myThread.Name & " has been relased!")
    
        Console.ReadKey()
      End Sub
    
      Public Sub Running()
        mut.WaitOne()
        Console.WriteLine("Thread is about to sleep!")
        Thread.Sleep(10000)
        Console.WriteLine("Thread is done sleeping!")
        mut.ReleaseMutex()
      End Sub
    End Module

    Did I make proper use of the Mutex class?  I am trying to move away from Thread.Suspend() and Thread.Resume().

    Thursday, November 11, 2010 2:31 PM

Answers

  • Hello wayne.blackmon,

    Thanks for your post.

    Yes. Thread.Suspend() and Thread.Resume() are deprecated as it may cause a deadlock to happen. The suspend() method doesn't release the acquired monitors. If a suspended thread has already acquired the monitor of a critical resource then an attempt to acquire the same resource in the thread would resume the suspended target thread which will cause a deadlock.

    Other alternatives, we could use various thread synchronization mechanisms available.  For example, the Monitor class (used implicitly by the "lock" statement), the various WaitHandle sub-classes (e.g. AutoResetEvent), or task-specific synchronization classes (e.g. Mutex, ReaderWriterLockSlim, etc.).

    You could also check below links for more ideas:
    http://www.c-sharpcorner.com/UploadFile/mmehta/Multithreading311162005045743AM/Multithreading3.aspx
    (Multithreading Part 3: Thread Synchronization)
    http://msmvps.com/blogs/peterritchie/archive/2006/10/13/_2700_System.Threading.Thread.Suspend_280029002700_-is-obsolete_3A00_-_2700_Thread.Suspend-has-been-deprecated_2E00__2E00__2E00_.aspx

    As far as I know, Mutex  is used when you need to synchronize across multiple processes to gain access to a common resource among several programs that you have written. It has two states: locked and unlocked. Once a mutex has been locked by a thread, other threads attempting to lock it will be blocked. When the locked thread unlocks (releases) the mutex, one of the blocked threads will acquire (lock) it and proceed. We use WaitHandle.WaitOne to request ownership of a mutex and call ReleaseMutex method the same number of times to release ownership of  the mutex.

    If you have any concerns, please feel free to follow up.

    Best regards,
    Liliane
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us. Thanks
    • Proposed as answer by Cor Ligthert Friday, November 12, 2010 7:54 AM
    • Marked as answer by wayne.blackmon Friday, November 12, 2010 2:57 PM
    Friday, November 12, 2010 7:18 AM