开发者

Quit main loop maybe the thread is still running

开发者 https://www.devze.com 2023-02-20 00:48 出处:网络
Hi all~ I have a problem boring me so much. Sometimes when I exit my program, there are some thread still running, in Linux system, it will cau开发者_如何学编程se crash after I quit the main loop. Is

Hi all~ I have a problem boring me so much. Sometimes when I exit my program, there are some thread still running, in Linux system, it will cau开发者_如何学编程se crash after I quit the main loop. Is there any method that can kill all threads when I quit main loop?


It would help a lot if you specified your programming language and threading library of choice.

The usual way to control this type of situation (that is for a parent thread to wait until children complete before terminating) is to call a function supplied by the library, usually named join or wait.

pthread supplies you with pthread_join, for example.

If you're spawning processes via fork, you should use wait or waitpid in the parent to halt until the child completes - try man waitpid or take a look at this.

This way you can inform your children that you are about to exit via the usual means, wait until they wrap up and terminate, then cleanly exit the main loop.

Does this help? This is the least brutal way of synchronizing termination, if you want to actively kill the children threads there are alternatives, of course (like pthread_kill for pthreads, for example).


If you are using java try using the jconsole (Java Monitoring & Management Console) shipped with jdk6u23 in my case. You can get the thread name that is not killed. You can use join for that thread to complete.

But there can be program issue like, in my case i had a timer thread hanging [Timer-0] java.util.Timer to make an a timer.cancel() which closed that timer.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号