none
How to suspend and resume a thread RRS feed

  • Question

  • Hi everybody,
    I'm writing a class that has 2 concurrent threads A and B. In thread A, there is a method that needs to suspend thread B when executing. The thread B is resumed after the method completes. My suggestion is to declare a private static variable  in my class such as "private static object myLock;" and to use "lock(myLock){}"  in my method.  I wonder  whether it's right. Please help me to solve this problem. Thank you .

    Thursday, March 27, 2008 2:11 AM

Answers

  • That should work.  There are other ways though too.  Here is a good article on thread synchronization.  If its just a method you can use the MethodImpl attribute.  The way your doing it is probably best though.


    http://www.albahari.com/threading/part2.html


    [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.Synchronized)]

    // same as
    lock ( typeof(MyClass ) { };


    Thursday, March 27, 2008 2:25 AM
  • You should not use Thread.Suspend/Resume as these are deprecated and don't work for thread syncing.

     

    The lock statement is the best/simplest approach although you could also use a wait object.  In general the Synchronize attribute is not a good choice for syncing threads as it applies to the entire method.  Rarely should an entire method body require serialized access.  A thread routine should never have this applied as it doesn't make any sense. 

     

    If you have a case where you need to suspend a thread while another thread runs then I'd wager you have a design issue.  It defeats the purpose of having threading if you have to suspend one thread to allow another to run.  A single thread would be more efficient.  It is more likely that you need to protect access to a particular shared resource.  In this case use the lock statement to protect access to that resource only as long as it is needed.  The longer a lock is held the less benefit you gain from threading. 

     

    There are quite a few ways to be thread safe without requiring serialized access to data.  The atomic operations supplied by Interlocked are the most efficient when they can be used.  Alternatively copying data to local, stack storage removes thread safety issues but can introduce other problems if that data has to be eventually written back.  Without knowing more about why you need to suspend your thread I can't provide any suggestions on how to better implement it to avoid the need for suspending the thread to begin with.

     

    Michael Taylor - 3/27/08

    http://p3net.mvps.org

     

     

    Thursday, March 27, 2008 1:50 PM
    Moderator

All replies

  • That should work.  There are other ways though too.  Here is a good article on thread synchronization.  If its just a method you can use the MethodImpl attribute.  The way your doing it is probably best though.


    http://www.albahari.com/threading/part2.html


    [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.Synchronized)]

    // same as
    lock ( typeof(MyClass ) { };


    Thursday, March 27, 2008 2:25 AM
  • You can create a delegate that handles your method. Call BeginInvoke method that will execute your function and make your second thread suspended (Thread.Suspend() ) until the EndInvoke is called then call thread resume method (Thread.Resume() ).

     

     

     

    Thursday, March 27, 2008 3:01 AM
  • Thats no good.  If you read his question his problem has to do with concurrency.  He wants only one thread at a time to be able to execute a method he has.  He should just use a lock block let he planned.
    Thursday, March 27, 2008 3:09 AM
  • Why not overload your method and pass the second thread?

     

      public void someMethod(arguments)

      {

    //  this is the method you had

      }

     

     public void someMethod(arguments, Thread trB)

     {

    trB.Suspend();

    someMethod(arguments)

    trB.Resume();

     }

    Thursday, March 27, 2008 10:53 AM
  • Forget what I wrote, block is definitely the safest way to go here!

    Thursday, March 27, 2008 11:03 AM
  • You should not use Thread.Suspend/Resume as these are deprecated and don't work for thread syncing.

     

    The lock statement is the best/simplest approach although you could also use a wait object.  In general the Synchronize attribute is not a good choice for syncing threads as it applies to the entire method.  Rarely should an entire method body require serialized access.  A thread routine should never have this applied as it doesn't make any sense. 

     

    If you have a case where you need to suspend a thread while another thread runs then I'd wager you have a design issue.  It defeats the purpose of having threading if you have to suspend one thread to allow another to run.  A single thread would be more efficient.  It is more likely that you need to protect access to a particular shared resource.  In this case use the lock statement to protect access to that resource only as long as it is needed.  The longer a lock is held the less benefit you gain from threading. 

     

    There are quite a few ways to be thread safe without requiring serialized access to data.  The atomic operations supplied by Interlocked are the most efficient when they can be used.  Alternatively copying data to local, stack storage removes thread safety issues but can introduce other problems if that data has to be eventually written back.  Without knowing more about why you need to suspend your thread I can't provide any suggestions on how to better implement it to avoid the need for suspending the thread to begin with.

     

    Michael Taylor - 3/27/08

    http://p3net.mvps.org

     

     

    Thursday, March 27, 2008 1:50 PM
    Moderator