Perl - 捕获意外终止
我有一些代码可以一次生成 5 个线程。我分离这些线程,但有一个共享变量 $THREADCOUNT
,我在线程调用调用的子过程开始时递增该变量,并在线程结束时递减。当 $THREADCOUNT 等于 0 时,我会再生成 5 次。
问题是,有时线程会意外退出,并且 $THREADCOUNT
永远不会使其变为 0,因此程序会停止。有没有办法捕获这样的退出并在意外退出时使用 $THREADCOUNT--
?
非常感谢。这是我的第一篇文章,如果有不清楚的地方,敬请谅解。
克里斯
I have some code that spawns 5 threads of itself, at a time. I detach those threads, but have a shared variable $THREADCOUNT
that I increment at the beginning of the subprocedure that is called by the thread call, and decrement at the end of the thread. When $THREADCOUNT
equals 0, I spawn another 5 times.
The problem is, sometimes the thread exits unexpectedly and the $THREADCOUNT
never makes it to 0, so the program stops. Is there someway to capture an exit like this and have $THREADCOUNT--
on unexpected exit?
Thanks so much. This is my first post so appologies if it's a little unclear.
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
共享
$THREADCOUNT
变量真的有必要吗?调用threads->list(threads::running)
将告诉您是否有任何spawn仍在运行。Is the shared
$THREADCOUNT
variable really necessary? A call tothreads->list(threads::running)
will tell you whether any of your spawn are still running.我不知道什么时候有人会想要使用
detach
。我会使用类似“请注意”的方法,这可以解决代码中的问题,即线程在启动后短时间内不会被计为启动。
I can't figure out when anyone would ever want to use
detach
. I'd use something likeNote that this fixes the problem in your code where a thread isn't counted as started for a short while after it has started.