在.NET中,当中止线程时,这段代码会被损坏吗?

发布于 2024-08-29 04:40:39 字数 874 浏览 15 评论 0原文

小介绍:

在复杂的多线程应用程序(企业服务总线ESB)中,我需要使用Thread.Abort,因为这个ESB接受用户编写的与硬件安全模块通信的模块。因此,如果该模块陷入僵局或硬件停止响应 - 我需要卸载该模块,并且该服务器应用程序的其余部分必须保持运行。

因此存在中止同步机制,确保代码只能在用户部分中中止,并且该部分必须标记为 AbortAble。如果发生这种情况(中止),则这段代码中可能会抛出 ThreadAbortException:


public void StopAbortSection()
        {
            var id = Thread.CurrentThread.ManagedThreadId;
            lock (threadIdMap[id])
            {
                ....
            }
        }

例如模块位于 AbortSection 中(通过调用类似的 StartAbortSection 方法进入)并且 ServerAplication 决定中止用户模块,但在此决定之后但在实际线程之前.Abort,模块通过调用此方法进入NonAbortableSection,但实际上对该锁定对象进行了锁定。

因此,lock 将阻塞,直到执行 Abort,但在到达此代码中的此块之前也可以执行 abort。但是使用这种方法的对象是必不可少的,我需要确保这段代码可以在任何时刻安全地中止(不会被损坏 - 例如,我不知道从字典中读取时会发生什么......)。

所以我不得不提到threadIdMap是Dictionary(int,ManualResetEvent),而锁定对象是ManualResetEvent的实例。

我希望你现在明白我的问题。抱歉它太大了。

Little intro:

In complex multithreaded aplication (enterprise service bus ESB), I need to use Thread.Abort, because this ESB accepts user written modules which communicates with hardware security modules. So if this module gets deadlocked or hardware stops responding - i need to just unload this module and rest of this server aplication must keep runnnig.

So there is abort sync mechanism which ensures that code can be aborted only in user section and this section must be marked as AbortAble. If this happen (abort) there is possibility that ThreadAbortException will be thrown in this pieace of code:


public void StopAbortSection()
        {
            var id = Thread.CurrentThread.ManagedThreadId;
            lock (threadIdMap[id])
            {
                ....
            }
        }

For example module is in AbortSection (entered by calling similar method StartAbortSection) and ServerAplication decides to abort user module, but after this decision but before actual Thread.Abort, module enters NonAbortableSection by calling this method, but lock is actualy taken on that locking object.

So lock will block until Abort is executed but abort can be executed also before reaching this block in this code. But Object with this method is essential and i need to be sure that this pieace of code is safe to abort in any moment (doesnt get corrupted - for example i dont know what happens when reading from Dictionary..).

So i have to mention that threadIdMap is Dictionary(int,ManualResetEvent), and locking object is instance of ManualResetEvent.

I hope you now understad my question. Sorry for its largeness.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诗酒趁年少 2024-09-05 04:40:39

“违反”例外是什么意思?

ThreadAbortException 可以在代码中的任何位置抛出。锁根本不会影响这一点,除非一个线程正在中止另一个线程,中止线程的代码位于锁内部,并且线程锁定同一对象。

线程必须锁定同一对象才能有效。除非字典中有不同的项目包含对相同 ManualResetEvent 对象的引用,否则锁完全没有用。

What do you mean by the exception being "violated"?

The ThreadAbortException can be thrown anywhere in the code. The lock doesn't affect that at all, unless it's one thread that is aborting another, the code to abort a thread is inside the lock, and the threads lock on the same object.

The threads have to lock on the same object to be effective. Unless you have different items in the dictionary that contain references to the same ManualResetEvent objects, the lock is totally useless.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文