Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Threading: Removing an element from a shared array

by P0w3rK!d (Pilgrim)
on May 15, 2003 at 20:45 UTC ( #258523=perlquestion: print w/replies, xml ) Need Help??

P0w3rK!d has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I have a process with several threads of which each needs to eliminate an element from a shared array when it is done with it. It is referencing the elements by index only. The thread number is the same as the index. I have the threading working, my class and its methods are working, but the array manipulation is not.

How do you remove an element from a shared array?

our @aryItems : shared; # Update: line added after initial posting # Here is the Foo class package Foo; use threads; # pull in threading routines use threads::shared; # and variable sharing routines # create a new Foo object sub new { my $pkg = shift; my $self = { @_, ... }; return bless($self, $pkg); } sub run { my $self = Foo->new(@_); my $id = $self->{id}; ... print "Hello from thread $id.\n"; print "Item $aryItems[$id]\n"; splice(@aryItems, $id); return 1; }
In Foo::run() I cannot get the splice to work. The output/error is as follows:
Hello from thread 0. Item 1153.xml thread failed to start: Splice not implemented for shared arrays at C: +/Perl/lib/ threads/shared.pm line 37. Hello from thread 1. Item 400.xml thread failed to start: Splice not implemented for shared arrays at C: +/Perl/lib/ threads/shared.pm line 37. Hello from thread 2. Item 1175.xml thread failed to start: Splice not implemented for shared arrays at C: +/Perl/lib/ threads/shared.pm line 37. Hello from thread 3. Item 1238.xml thread failed to start: Splice not implemented for shared arrays at C: +/Perl/lib/ threads/shared.pm line 37.

Thanks :)

-P0w3rK!d

Added line per author - dvergin 2003-05-15

Replies are listed 'Best First'.
Re: Threading: Removing an element from a shared array
by BrowserUk (Pope) on May 15, 2003 at 21:01 UTC

    Splice not implemented for shared arrays at...

    Don't use splice. Use delete $aryItms[$id] instead.

    You should also be using lock to prevent more than one thread trying access the same array element at a time.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

        You shouldn't need the call to cond_signal(...) unless you are trying to deliberately meter the flow of your threads (a bad idea in most cases), or possible if you are going to need to re-access the same variable later at the same scope, Otherwise, and in most cases, you only need to let the lock go out of scope for the shared variable to become unlocked.

        Also, you (probably) shouldn't be locking the whole array, just the element that you want to access.

        sub run { my $self = Foo->new(@_); my $id = $self->{id}; ... print "Hello from thread $id.\n"; lock($aryItems[$id]); print "Item $aryItems[$id]\n"; delete $aryItems[$id]; # The lock is removed once when the function returns return 1; }

        There is a caveat of using delete, unlike with splice the array element is not removed, it is effectively just undefed.

        It is difficult to be more specific without understanding more about what it is that your code is trying to achieve. I realise that you probably just exploring the possibilities at the moment, but there are different ways of using threads depending upon your ultimate goal.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
Re: Threading: Removing an element from a shared array
by rinceWind (Monsignor) on May 16, 2003 at 10:10 UTC
    This seems remarkably similar to the question I raised in thread Atomic operations in perl and Tk::IO, albeit my problem was with Tk events, not threads.

    Do you think there is a case for a new module to atomify an array? Is it asking too much to be compatible with different versions of perl? I know that many of the issues of thread safety and atomicity vary greatly betwen versions.

      >>Do you think there is a case for a new module to atomify an array?

      Possibly.

      >>Is it asking too much to be compatible with different versions of perl? I know that many of the issues of thread safety and atomicity vary greatly betwen versions.

      I would say yes in most cases. Why you ask? See this. There is no reason not to use the latest version of Perl, but in some cases you are forced to support multiple versions.

      -P0w3rK!d

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://258523]
Approved by Ovid
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2019-12-08 10:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?