Skip Menu |
 

This queue is for tickets about the threads-shared CPAN distribution.

Report information
The Basics
Id: 39633
Status: rejected
Priority: 0/
Queue: threads-shared

People
Owner: Nobody in particular
Requestors: k.LabMouse [...] gmail.com
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: 1.26
Fixed in: (no value)



Subject: Can't share ref 'CODE'.
Download (untitled) / with headers
text/plain 214b
Distribution: threads-shared-1.26 Perl version: 5.8.8 OS: Win XP x86 When I try to share ref type 'CODE' it fails. Message is: "Cannot share subs yet". P.S. BTW, there is nothing about that limitation in perldoc.
Subject: test.pl
Download test.pl
text/x-perl 405b
#!/usr/bin/perl use strict; use warnings; use threads; use threads::shared; our $funcref :shared; # Pointer to Function. Eg. "CODE". sub dummy { print "I\'m a dummy sub\n"; } print "Trying to set shread value to CODE.\nIf this don\'t print \"WOW\", then we failed.\n"; lock ($funcref); my $new_funcref = \&dummy; $funcref = share($new_funcref); print "WOW.\n"; $funcref->();
Download (untitled) / with headers
text/plain 515b
On Fri Sep 26 13:30:43 2008, kLabMouse wrote: Show quoted text
> Distribution: threads-shared-1.26 > Perl version: 5.8.8 > OS: Win XP x86 > > When I try to share ref type 'CODE' it fails. > Message is: "Cannot share subs yet". > > P.S. BTW, there is nothing about that limitation in perldoc.
I beg to differ. From 'perldoc threads::shared': Show quoted text
> This module supports the sharing of the following data types > only: scalars and scalar refs, arrays and array refs, and > hashes and hash refs.
It doesn't get any clearer than that.
From: k.LabMouse [...] gmail.com
Download (untitled) / with headers
text/plain 886b
Птн. Сен. 26 13:36:42 2008, JDHEDDEN писал: Show quoted text
> On Fri Sep 26 13:30:43 2008, kLabMouse wrote:
> > Distribution: threads-shared-1.26 > > Perl version: 5.8.8 > > OS: Win XP x86 > > > > When I try to share ref type 'CODE' it fails. > > Message is: "Cannot share subs yet". > > > > P.S. BTW, there is nothing about that limitation in perldoc.
> > I beg to differ. From 'perldoc threads::shared': >
> > This module supports the sharing of the following data types > > only: scalars and scalar refs, arrays and array refs, and > > hashes and hash refs.
> > It doesn't get any clearer than that.
YES. but I can't use Event Handler because of that =(( I use global Thread called "Interface", and when some message pop's up, it must call a registered function from other module. Registering can be made from any currently running Threads, but that's impossible at this time. =(
Download (untitled) / with headers
text/plain 724b
Show quoted text
> YES. but I can't use Event Handler because of that =(( > I use global Thread called "Interface", and when some message pop's up, > it must call a registered function from other module. > Registering can be made from any currently running Threads, but that's > impossible at this time. =(
Sorry, but it's a limitation with Perl. Shared data is implemented using ties. Code refs cannot be used with ties. Additionally, I seems to me that mixing threads with events is a bad paradigm combination. One suggestion I can offer (albeit I'm quite ignorant of your current implementation) would be to register Thread::Queue's instead of functions. I've attached a crude example script to illustrate what I mean. Good luck.
Download callback.pl
text/x-perl 1.9k
#!/usr/bin/perl use strict; use warnings; use threads; use Thread::Queue; # Queue for registering callbacks my $regis_q = Thread::Queue->new(); # Queue for disseminating events my $event_q = Thread::Queue->new(); MAIN: { # Capture SIGUSR1 events $SIG{'USR1'} = sub { $event_q->enqueue('USR1'); # Send to event handler }; # Capture SIGUSR1 events $SIG{'USR2'} = sub { $event_q->enqueue('USR2'); # Send to event handler }; # Create callback threads threads->create('CallBack', 'USR1')->detach(); threads->create('CallBack', 'USR2')->detach(); # Create event handler threads->create('EventHandler')->detach(); # Ready print("Send signals to PID = $$\n"); print("Use ^C to terminate\n"); # Just hang around while (1) { sleep(10); } } ### Subroutines ### sub EventHandler { my %callbacks; # Registered callback queues while (1) { # Wait for event my $event = $event_q->dequeue(); redo if (! $event); # Check for any registrations while (my ($event_type, $q) = $regis_q->dequeue_nb(2)) { if ($q) { $callbacks{$event_type} = $q; } else { warn("Bad callback registration for event type $event_type"); } } # Send event to appropriate queue $callbacks{$event}->enqueue($event); } } sub CallBack { my $event_type = shift; # The type of event I'm handle # Announce registration my $tid = threads->tid(); print("Thread $tid registering for event type $event_type\n"); # Register my queue for my type of event my $q = Thread::Queue->new(); $regis_q->enqueue($event_type, $q); # Process loop while (1) { # Wait for event callback my $item = $q->dequeue(); # Process event print("Callback thread ($tid) got '$item'\n") if $item; } } # EOF


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.