gpt4 book ai didi

perl - 如何在 Perl 中的线程之间共享对象?

转载 作者:行者123 更新时间:2023-12-03 12:53:24 24 4
gpt4 key购买 nike

我已经搜索了相关主题,但仍然无法解决问题...

use threads;
my $person = new Person( 'Name' => "yy");

my $udp_thread = threads->new(\&udp_func);

while(1)
{
$person->working();
}

sub udp_func
{
#Can't call method "setName" on an undefined value:
$person->setName();
}

如何在新线程中访问对象 $person ?非常感谢!!

最佳答案

你看过threads::shared了吗? Perl 扩展?

threads::shared - Perl extension for sharing data structures between threads. By default, variables are private to each thread, and each newly created thread gets a private copy of each existing variable. This module allows you to share variables across different threads (and pseudo-forks on Win32). It is used together with the threads module. This module supports the sharing of the following data types only: scalars and scalar refs, arrays and array refs, and hashes and hash refs.

关于perl - 如何在 Perl 中的线程之间共享对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1706304/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com