1

I have Perl Tk application which uses a repeater (mw->repeat).

The repeater is calling a function which refreshes some Tk objects inside the GUI.

This repeater function is taking some time to execute - hence it freezes the application for several seconds. I'm also storing objects inside hashes and therefore I'm trying to configure "complex" data structures inside the repeater.

I've been looking for a way to execute this function on the background (possibly thread?) but unfortunately I could not make it.

I understand that I need to use threads::shared. However, when I'm trying to assign the objects into the hash it won't let me, saying "Invalid value for shared scalar".

One of the experiments I've tried:

#main
our %hash :shared;
my %p,%p2;
$hash{$key} = \%p;
$hash{$key}{$key2} = \%p2;
$hash{$key}{$key2} = Button(...)

#inside the repeater - with threads
$hash{$key}{$key2}->configure(-text => "foo");

What do I miss here?

Thanks.

orohev
  • 73
  • 11
  • I guess [Tk::after](https://metacpan.org/dist/Tk/view/pod/after.pod) is not an option then? Maybe [AnyEvent](https://metacpan.org/pod/AnyEvent) could be used? – Håkon Hægland Apr 11 '22 at 16:08
  • According to [this](https://www.perlmonks.org/?node_id=732294) post, Tk is not thread safe, but threads can be used if done with precaution – Håkon Hægland Apr 11 '22 at 16:15
  • *"However, when I'm trying to assign the objects into the hash it won't let me"* : Have you tried using [shared_clone()](https://perldoc.perl.org/threads::shared), see [this](https://stackoverflow.com/a/68879939/2173773) answer for more information – Håkon Hægland Apr 11 '22 at 16:32
  • 2
    Also do not use Tk code from the thread. According to [this](https://www.perlmonks.org/?node_id=732320) answer: *"Do not put any Tk code into the thread, and do not try to access Tk widgets from the thread. Use shared variables to communicate with the main thread, and have a timer or fileevent in the main Tk thread, read from the thread."* – Håkon Hægland Apr 11 '22 at 16:53
  • Thank you. Regarding AnyEvent - I did not exactly understand the application here. shared_clone unfortunately did not work for me, as the original hash should be shared as well. So to my best understanding the only way I have here is to hash in (shared) the desired changes inside the thread and then in the main loop do the actual changes when the thread completes his work? – orohev Apr 12 '22 at 16:28
  • 1
    *"shared_clone unfortunately did not work for me, as the original hash should be shared as well"*: Could you create a minimal example that demonstrates the issue? See [mcve] for more information – Håkon Hægland Apr 12 '22 at 17:58

0 Answers0