Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Sharing a Thread::Semaphore among threads

by philou (Beadle)
on Sep 29, 2004 at 08:25 UTC ( #394885=perlquestion: print w/replies, xml ) Need Help??

philou has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I've got a bit of trouble with perl threads. What I'm trying to do is to share a Thread::Semaphore object among multiple threads ( actually I would like to embed a Thread::Semaphore within an object ). The straigthforwad approach does not work :
#!/home/philou/install/perl-5.8.2-threads/bin/perl -w use strict; use threads; use threads::shared; use Thread::Semaphore; my $s = 'Thread::Semaphore'->new( 1 ); sub t { while( $s->down() ) { print "DOWN!\n"; sleep( 1 ); } } 'threads'->new( \&t ); while( <> ) { $s->up(); print "UP!\n"; }
That is, each time I press the `Enter' key, I would expect a "DOWN!" to show up on my terminal ( if the semaphore was shared ), but nothing happens.

Declaring the `$s' variable as `shared' does not help at all :

#!/home/philou/install/perl-5.8.2-threads/bin/perl -w use strict; use threads; use threads::shared; use Thread::Semaphore; my $s : shared = 'Thread::Semaphore'->new( 1 ); sub t { while( $s->down() ) { print "DOWN!\n"; sleep( 1 ); } } 'threads'->new( \&t ); while( <> ) { $s->up(); print "UP!\n"; }
When excuting the previous piece of code, it seems that the sharedness of `$s' prevents it from being a blessed object, and I end up with :
thread failed to start: Can't call method "down" on unblessed referenc +e at ./ line 11.
So my question is: How do you share Thread::Semaphore among perl threads ?

Any suggestion appreciated.


Replies are listed 'Best First'.
Re: Sharing a Thread::Semaphore among threads
by Roger (Parson) on Sep 29, 2004 at 11:54 UTC
    Due to Perl's prototyping "problem", when you want to share a reference to an object, you need to do something like:

    my $s = &shared( Thread::Semaphore->new );

    Note that you don't have to initialize the semaphore with counter of 1, because it is set to 1 by default.

Re: Sharing a Thread::Semaphore among threads
by BrowserUk (Pope) on Sep 29, 2004 at 14:07 UTC

    First off, I've never used Thread::Semaphore.

    Part of the reason I've never used it is because to my knowledge, you cannot share objects between threads, but for Thread::Semaphore to be useful, you would need to be able to invoke the methods from multiple threads? And I don't believe that this will work.

    From my perspective, almost everything in the Thread::* namespace was written for use with perl5005threads, which are now deprecated in favour of iThreads, because they never really worked properly.

    As such, I think that everything in the Thread::* namespace should be withdrawn or if it is really iThreads compatible, be renamed into the threads::* namespace so that it becomes clear which of those packages in Thread::* are actually usable with iThreads.

    Of course, this won't happen because the namespace nazis will say that having packages where the first element of the name is all lowercase will confuse people, because all lowercase is reserved for pragmas. The fact that 90% of the modules in the Thread::* namespace were never designed or tested for use with iThreads, which just makes people that try to use them with iThreads think that iThreads are broken. But then, most of those same "namespace nazis" don't believe that Perl should have ever had threads in the first place, wouldn't use them or promote them on principle, and therefore don't give a flying f*** about the confusion this stupid decision causes.

    Of course, never having tried to use Thread::Semaphore (I never saw a the need for it), I could be completely wrong about it. Thread::Queue does work and I couldn't live without that module. Even so, the gist of my rant above remains true regardless.

    Until something is done to segregate those modules that are designed, for and tested with, iThreads; from those that are left overs from 5005threads that will never work; from those that were designed to be used with Forks (a mechanisms for bypassing threads completely), that have never been tested for use with threads proper, all of the heroic effort by Arthur Bergman to get iThreads to where they are now will tend to be wasted, because people will be trying to use the wrong modules with iThreads and fall foul of the stupid namespace decisions.

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2020-10-20 06:35 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (209 votes). Check out past polls.