在下面的片段中,如何区分我的子 foo
的第二个调用实例从一开始?
while ($whatever) {
foo(); foo(); # foo() and foo() have the same caller package, file, and line
}
有点像 super -
caller()
返回的文件、行和列可以解决问题。我不想使用源过滤器。背景,或者,这不是一个 XY 问题吗?
我有一个便利模块 Local::Thread::Once,它公开了像
pthread_once
这样的功能。/std::call_once
以面向对象的方式,也作为子程序属性。这些都很容易,因为在任何一种情况下都有一个自然且明确的“once_control”或“once_flag”。但是,还有一个程序接口(interface) —
once { ... }
— 目前基于 $filename
进行序列化和 $line
由 caller
返回.像这样的东西:sub once(&) {
my $user_routine = shift;
my (undef, $file, $line) = caller;
my $once_control = get_a_shared_flag_just_for_this_invocation($file, $line);
lock($once_control);
if (! $once_control) { $once_control++; $user_routine->(); }
return;
}
这并不是它的工作原理——真正的效率更高——但重点是调用是从调用者的文件和行中分离出来的。这有效,只是它无法区分同一行上的两个调用。
while ($whatever) {
once { foo(); }
once { bar(); } # OK, foo() and bar() each called only once
once { baz(); }; once { buz(); }; # :( buz() not called, not even once
}
注意
$user_routine
的地址不能用作附加判别式,因为 subs 是从一个 ithread 复制到另一个 ithread。我可以忍受这个问题作为一个非常人为的用例的记录限制,但我更愿意以某种方式修复它。
最佳答案
Devel::Callsite正是为此目的而编写的。
关于multithreading - 区分一个子调用和另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18494705/