Terminating plugin thread

Started by
3 comments, last by the_edd 16 years, 6 months ago
At the moment I'm programming a plug-in system for some software and am a little confused about a threading issue. Plug-ins can be made by anyone so I can't make any assumptions about them; they could simply loop infinitely and I should be able to deal with it. Now to handle some plug-in functionality I start a new thread which runs a third-party function. My problem is: What if I want to shut down my application while the function is still running? I don't want to just terminate it since it could have allocated system resources, being in the process of doing IO, etc. At the same time I can't just wait for it to finish since I have no reason to believe that it will finish in the near future. I could just wait for some fixed time (say 5 seconds) and then terminate it if it isn't done yet, but there are legitimate situations where the function takes about a day before finishing (waiting for external events). My initial thought was to have a call-back that tells the plug-in that we're terminating now and that it should have returned from all its functions in x seconds. If it doesn't then it will be terminated. I just don't like the idea of this since if x is too large the user will be annoyed and if x is too small I'll risk that some real plug-ins won't get to finish. At the same time it gives the plug-in programmers yet another responsibility. So can anyone think of a better way to handle this?
Advertisement
Why should the plugin get to decide when to quit, instead of the user deciding? If I quit Photoshop with a Gaussian blur running, chances are I don't care about the result of the gaussian blur anyway.
Quote:Original post by Sneftel
Why should the plugin get to decide when to quit, instead of the user deciding? If I quit Photoshop with a Gaussian blur running, chances are I don't care about the result of the gaussian blur anyway.


Because the plugin may have acquired system resources, so we may end up with resource leaks if we don't let it release resources. It may also be in the process of doing file IO so we could end up with corrupted files because it didn't finished writing.

EDIT: I should perhaps add that these plugins could do stuff like simple source control or automatic backup, so access to the file system, network and some OS resources are required.
To remove the user annoyance factor, it's easy enough to half-close by hiding the window, and freeing any resources that aren't being used by plugins. I believe uTorrent does something like this.
You don't have to run a loop inside the plugin. Have a different thread inside your application that does the looping, calling in to the plugin each time around.

If your app wants to quit, shut down the loop (which is easy to do now) and call your plugin's clean-up function.

This topic is closed to new replies.

Advertisement