线程同步问题:中止具有锁定资源的线程 [英] Thread Synchronization Issue: Aborting thread with locked resources

查看:98
本文介绍了线程同步问题:中止具有锁定资源的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.我有一个多线程服务应用程序,其中许多线程访问一个公共资源.我有一个典型的Readers/Writers环境,其中许多线程可以同时读取,但是只有一个线程可以专有地写入资源.

事实是,每当两个线程(或更多线程)请求写许可时,我都不需要等待第一个线程完成,就可以中止它(不再需要第一个线程)并执行第二个线程.由于写操作是一项昂贵的操作,因此最好中止第一个线程并执行第二个线程.

所以,这是我的疑问:如果我中止持有锁的线程,它将释放锁吗?

我的简化代码如下所示:

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();
   }
}



所以这是交易.我为所有人提供了一个固定的通用写线程,因此我可以测试该线程是否已在运行,如果已经运行,则可以中止并重新启动它.这具有中止第一次写入并执行第二次写入的效果.每当线程需要写入资源时,它仅调用TryWrite函数.原始调用线程不需要等待操作结束,因此它返回并完成.

但是,由于写入操作"需要很长时间,因此WriteThread在写入时(即在其持有锁时)更有可能中止.

该代码行吗?或者在编写过程中线程中止时,它将死锁?



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?

推荐答案



中止线程是个坏主意,因为它(1)可能不会立即发生(例如,在内部时) (某些I/O操作)和(2)会在未知状态下被踢出,而没有机会自行清除.

因此建议不要完全中止,而要实施合作线程退出方案,其中线程定期检查标志以确保其应继续工作或退出.

由于您将中止线程以启动类似线程,因此无需执行任何线程操作只需告诉线程放弃其当前作业并开始执行新作业即可.

:)

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.

:)


尝试使用并行编程对其进行编程良好的线程管理.这是在4.5 .net框架中使用的.
Try to use Parallel programming its good thread management.this is used in 4.5 .net framework.


我不知道您的应用程序在做什么,但您可能还会考虑计时器的想法...
如果工作线程正在写入数据,则可以暂停计时器,并在处理完成后重新启动计时器.

同样,您的应用可能并非如此.

我已经用线程完成了文件侦听器过程.
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.


这篇关于线程同步问题:中止具有锁定资源的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆