螺纹的轻量叉替换

编程入门 行业动态 更新时间:2024-10-11 17:22:41
本文介绍了螺纹的轻量叉替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在下面使用类似于 threads async 的 fasync.我是不是忘记了什么,这里是否有一些特殊情况没有涵盖?

I would like to use fasync below similar to threads async. Am I forgetting something, are there some corner cases which are not covered here?

sub fasync(&) { my ($worker) = @_; my $pid = fork() // die "can't fork!"; if (!$pid) { $worker->(); exit(0); } return sub { my ($flags) = @_; return waitpid($pid, $flags // 0); } } my @join = map { my $job = $_; fasync { print "$job\n"; }; } 1 .. 10; $_->() for @join;

推荐答案

看起来它应该可以正常工作,尽管应该对 fork 进行错误检查(它返回 undef 失败),如果您想在子级和父级之间或不同子级之间使用任何通信方式,则需要详细说明.

That looks like it should work just fine, although there should be an error check on fork (it returns undef on failure), and it needs elaboration if you want any means of communicating between the children and the parent, or between different children.

更多推荐

螺纹的轻量叉替换

本文发布于:2023-11-25 10:59:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629484.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:螺纹   轻量叉

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!