JM:I have a dream,期待和你一起去实现!
原文及译文
JM
网友:twk

        我有一个C Win32应用程序,它有一些线程,当用户想要关闭应用程序时,可能会忙于IO(HTTP调用等)。目前,我打得很好,等待所有的线程结束之前,从主返回。 Sometim...

推荐:想去Google查资料?(你懂的)想建站?想测试?想挂机?想折腾?想要高性价比的服务器?点我!!


我如何保证我的win32应用程序快速关机?(How do I guarantee fast shutdown of my win32 app?)

我有一个C Win32应用程序,它有一些线程,当用户想要关闭应用程序时,可能会忙于IO(HTTP调用等)。目前,我打得很好,等待所有的线程结束之前,从主返回。有时,这需要比我想要的更长的时间,事实上,当我可以退出时,让用户等待似乎是毫无意义的。然而,如果我只是继续从main返回,那么当析构函数开始被调用,而仍然有线程使用这些对象时,我很可能会崩溃。

所以,认识到在一个理想的,柏拉图的美德世界里,最好的办法就是等待所有的线程退出,然后干净地关闭,下一个最好的现实世界的解决方案是什么?简单地使线程退出可能不是一个选择。目标是尽可能快地让程序停止,以便例如可以在其上安装新版本。我正在做的唯一的磁盘IO是在一个事务性的数据库,所以我并不担心拉上那个插件。


I've got a C++ Win32 application that has a number of threads that might be busy doing IO (HTTP calls, etc) when the user wants to shutdown the application. Currently, I play nicely and wait for all the threads to end before returning from main. Sometimes, this takes longer than I would like and indeed, it seems kind of pointless to make the user wait when I could just exit. However, if I just go ahead and return from {mark2}, I'm likely to get crashes as destructors start getting called while there are still threads using the objects.

So, recognizing that in an ideal, platonic world of virtue, the best thing to do would be to wait for all the threads to exit and then shutdown cleanly, what is the next best real world solution? Simply making the threads exit faster may not be an option. The goal is to get the process dead as quickly as possible so that, for example, a new version can be installed over it. The only disk IO I'm doing is in a transactional db, so I'm not terribly concerned about pulling the plug on that.


解决方案(由网友Roddy提供)

使用重叠的IO,以便始终控制处理I / O的线程,并始终可以在任何时候停止它们;你可以让他们在IOCP上等待,并可以发布一个应用程序级别的关闭代码,或者你可以等待OVERLAPPED结构中的事件,并等待你的“所有线程,请关闭”事件。

总之,避免阻止你不能取消的呼叫。

如果你不能,而且你被困在一个阻塞的套接字调用中,那么你总是可以关闭线程中的套接字,这个套接字已经决定了关闭的时间,并且让线程在做IO的时候总是检查'shutdown shutdown now '重试前的事件...


Use overlapped IO so that you're always in control of the threads that are dealing with your I/O and can always stop them at any point; you either have them waiting on an IOCP and can post an application level shutdown code to it, OR you can wait on the event in your OVERLAPPED structure AND wait on your 'all threads please shutdown now' event as well.

In summary, avoid blocking calls that you can't cancel.

If you can't and you're stuck in a blocking socket call doing IO then you could always just close the socket from the thread that has decided that it's time to shut down and have the thread that's doing IO always check the 'shutdown now' event before retrying...


推荐:想去Google查资料?(你懂的)想建站?想测试?想挂机?想折腾?想要高性价比的服务器?点我!!


关于站长

JMJavaMethod的缩写,苦逼码农一个,一直想有番作为,奈何人老力衰,只能四处膜拜大佬以获得动力。已经从单机、局域网、互联网、混到了移动互联网,未来希望能在AI世界里继续混下去。这辈子有个终极目标:财务自由,心灵自由。

近期公告

一个人苦逼开发多日,网站终于正式上线,求关注!!! 一个人苦逼多日,终于正式上线,求关注!!!

找他  
猜你喜欢
想建站?想测试?
想挂机?想折腾?
想去Google查资料?(你懂的)
想要高性价比的服务器?
搬瓦工VPS - 性价最高的美国便宜VPS主机
友情链接

冀ICP备17016304号 版权所有 © JavaMethod.com All Rights Reserved,Theme by 拼图