停止长时间操作的线程的正常实践是什么?
假设我即将关闭应用程序/停止服务,但是我的一个线程正在进行一个我无法控制的漫长操作(假设使用了三十个派对组件)。停止长时间操作的线程的正常实践是什么? Windows服务不喜欢长时间的操作(当我单击“停止按钮”时)。
Lets say I am about to close application/stop service, but one of my threads is going a long operation that I have no control of (lets say thirty party component is used). What is normal practice of stopping threads that do long operations? Windows service doesn't like long operations (when I click Stop button).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个深刻的问题。如果您的线程不合作,您正在要求一个麻烦的世界。杀死线程的问题是,您无法知道该线程在被杀死的那一刻在做什么。当它从堆分配一个物体的中间,您可以杀死它,这可能会使堆破坏。您的程序很快就会崩溃。您可以在一些互惠件被锁定时杀死它,而试图锁定同一静音的下一个线将悬挂。
IMO:最佳开箱即用的方法是在单独的过程中执行“长途操作”。设计可以安全杀死的过程容易得多。
That's a deep problem. You are asking for a world of trouble if your threads don't cooperate with each other. The problem with killing a thread is, you have no way of knowing what the thread was doing at the moment it was killed. You could kill it while it was in the middle of allocating an object from the heap, and that could leave the heap corrupted. Your program soon would crash. You could kill it while some mutex was locked, and the next thread that tried to lock the same mutex would hang.
IMO: the best way out of the box you're in would be to perform that "long operation" in a separate process. It's much easier to design a process that can be safely killed.