Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Accessing one perl sub by multiple threads..

by Anonymous Monk
on Dec 07, 2000 at 16:39 UTC ( #45489=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I've a doubt on accesssing one perl subroutine by multiple threads. Let us say two threads (using perlcall APIs) access a perl sub named X. First thread invoked X. while processing, First thread is preempted and the Second thread starts executing and accessing the same sub X. My question is, if the First thread continue execution from where it was preempted by the Second thread?. Is perl subroutine thread safe. We are getting some strange results. If you have answers for the same please let us know. Please contact me at Thanks, Poornachandran SM.
  • Comment on Accessing one perl sub by multiple threads..

Replies are listed 'Best First'.
(tye)Re: Accessing one perl sub by multiple threads..
by tye (Sage) on Dec 07, 2000 at 22:52 UTC

    Perl isn't thread safe. You'll need to do one of the following:

    • Use some sort of mutex to prevent simultaneous access to the Perl interpreter, making a "critical section" that only one thread can use at a time. I believe someone has already built this so you might be able to find it.
    • Create (a pool of) separate Perl interpreters for each thread that needs to call a Perl subroutines. This won't work for you if the subroutines need to act on shared data. Note that you can use the same Perl interpreter in different threads so long as the Perl interpreter is not built with threading support and only one thread uses that interpreter at a time.
    • Try embedding a Perl interpreter built with one of the several experimental methods for supporting multiple thread per interpreter and access the interpreter as required by that method. This will cause the interpreter to use mutexes to make critical sections around smaller chunks of code. But none of these work 100% reliably so you'll have to deal with that.
    There might be some other methods, but the above should give you a feel for what is required.

    Please contact me at

    Sorry, you'll have to check back here.

            - tye (but my friends call me "Tye")
Re: Accessing one perl sub by multiple threads..
by AgentM (Curate) on Dec 07, 2000 at 23:34 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://45489]
Approved by root
[Cosmic37]: now I have tried another blunder - can anyone explain why I am such a dunderhead?
[Cosmic37]: if ( $line =~ /$mydt/ ) { print $line; }
[Cosmic37]: I try to match successive date times stored in variable $mydt
[Cosmic37]: I guess it is searching for the string "$mydt"
[Corion]: Indeed cool, erix ;)
[Cosmic37]: rather than the value of $mydt which is a date time strong such as 2016-01-01 12:30:56
[Corion]: Cosmic37: No, but maybe $mydt doesn't contain what you think it does, or it contains characters that are special in a regular expression? Try if( $line =~ /\Q$mydt\E/) { ... for a literal match
[Cosmic37]: I mean string grrr
[Corion]: Maybe add an else branch in which you print what the values of $line and $mydt are?
[Cosmic37]: ah thank you I will try

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (11)
As of 2017-06-29 16:58 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (673 votes). Check out past polls.