in reply to Re: Thread-safe modules and callbacks
in thread Thread-safe modules and callbacks

Exactly that! Don't know if possible, it may be possible to access local variables of a function and that could let us delcare as shared dynamically callbacks variables. but that doesn't solve the problem when callback acess main threads variables.
  • Comment on Re^2: Thread-safe modules and callbacks

Replies are listed 'Best First'.
Re^3: Thread-safe modules and callbacks
by pc88mxer (Vicar) on May 09, 2008 at 23:30 UTC
    How about switching to objects, and create a closure for your callback:
    use threads::shared; my $obj = bless {}, 'foo'; sub foo::callback { warn "I've been called ".++($_[0]->{count})." times\n"; } share($obj); # only needed in a threaded environment my $thread = threads->create('start_thread', sub { $obj->callback(@_) +} ); $thread->join; print "count = ", $obj->{count}, "\n"; # prints 3 sub start_thread { my $cb = shift; for (1..3) { $cb->() } }
    Is there a way to implement function call behavior for a blessed reference? If so, you could have the best of both worlds.