Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
4.91/5 (9 votes)
See more:
Hi. I've a multithreaded service application with many threads accessing a common resource. I have a typical Readers/Writers environment, where many threads may read at the same time but only one can write to the resource, exclusively.

The thing is, whenever two threads (or more) asks for writing permission, I do not need to wait for the first thread to finish, I can just abort it (the first thread is no longer needed) and execute the second thread. Since writing is an expensive operation, it's desirable that I abort the first thread and execute the second.

So, here's my doubt: if I abort a thread that holds a lock, will it release the lock?

My simplified code looks like this:

private static System.Threading.Thread WritingThread =   
    System.Threading.Thread(MyClass.PerformWriting)

private void TryWrite()
{
   If (WritingThread.IsAlive)
      WritingThread.Abort();

   WritingThread.Start();
   
}

private void PerformWriting()
{
   MyReaderWriterLockSlim.EnterWriteLock();

   try{
      //writing operation - takes a long time
   }
   finally{
      MyReaderWriterLockSlim.ExitWriteLock();
   }
}


So here's the deal. I have a commom fixed writing thread for everybody, so I can test if the thread is already running, and if is, I can just abort it and initiate it again. This get the effect of aborting the first writing and executing the second. Whenever a thread needs to write to the resouce, it just calls the TryWrite function. The original calling thread doesn't need to wait the end of the operation, so it returns and finishes.

But, since "writing operation" takes a long time, it is more likely that the WritingThread will be aborted when it is writing, that is, when it holds the lock.

Is that code ok, or it will deadlock when the thread gets aborted in the middle of the writing?

Posted
Updated 13-Mar-10 16:33pm
v2

Hi,

aborting a thread is a bad idea, as it (1) may not occur right away (e.g. when inside some I/O operations) and (2) will get kicked out in an unknown state, without getting a chance for cleaning up after itself.

so the recommendation is not to abort at all, rather to implement a cooperative thread exit scheme, where the thread regularly checks a flag to make sure it should continue its work or exit.

And as you would abort a thread to start a similar thread, there is no need to do any thread operation at all, just tell the thread to abandon its current job and start executing the new job.

:)

 
Share this answer
 
Try to use Parallel programming its good thread management.this is used in 4.5 .net framework.
 
Share this answer
 
v2
Comments
[no name] 20-Jul-12 7:04am    
This might work if he was having this problem today but since he had this problem 3 years ago....
I don't know what your app is doing, but you may also look into timer idea...
If working thread is writing data you can pause the timer and re start it after process is completed.

again, that may not be the case for your app.

I have done it with my threads for file listener process.
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900