Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7400073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:08:52+00:00 2026-05-29T04:08:52+00:00

I just introduced threads to a Perl program, where one of its modules was

  • 0

I just introduced threads to a Perl program, where one of its modules was using Memoize.
I’m getting this error message:

Thread 1 terminated abnormally: Anonymous function called in forbidden scalar context; faulting

The error occurs if I have both threads and Memoize, but will disappear if I take away one of these elements. But the problem isn’t because Memoize isn’t thread-safe – in my code, all the memoization happens within the same thread.

Is this a bug with Memoize? Is there a way I can work around this? Otherwise I’m going to get rid of Memoize.

Here’s some sample code to isolate the problem:

use strict;
use warnings;
use threads;
use Thread::Semaphore;
use Memoize;

my $semaphore = Thread::Semaphore->new;

memoize('foo');
sub foo {
    return shift;
}

sub invoke_foo {
    $semaphore->down; # ensure memoization is thread-safe
    my $result = foo(@_);
    $semaphore->up;

    return $result;
}

my @threads;
foreach (1 .. 5) {
    my $t = threads->create( sub { invoke_foo($_) });
    push @threads, $t;
}
$_->join foreach @threads;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:08:53+00:00Added an answer on May 29, 2026 at 4:08 am

    Memoize stores the caches for every memoized function in one hash (instead of using a closure). It uses the address of the function as the index into that hash.

    The problem is that the address of the function changes when it’s cloned into a new thread. (Add print(\&foo, "\n"); in invoke_foo.). It’s a bug in Memoize.

    Workaround: Load the memoised module from within the threads. the following simulates (the relevant aspects of) that:

    use strict;
    use warnings;
    use threads;
    use Memoize;
    
    sub foo {
        return shift;
    }
    
    sub invoke_foo {
        return foo(@_);
    }
    
    my @threads;
    foreach (1 .. 5) {
        my $t = threads->create( sub {
            memoize('foo');
            invoke_foo($_);
        });
        push @threads, $t;
    }
    $_->join foreach @threads;
    

    By the way, each thread has its own cache. that could also be considered a bug.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just getting my head around Ruby metaprogramming. The mixin/modules always manage to confuse me.
I've just been introduced to traits (traits in Smalltalk, roles in Perl). I'd like
Im am just getting introduced to unit testing and test driven development. Thus far,
I just introduced multithreading into my app JUST to get a silly UIActivityIndicatorView to
I just introduced SVN in our company for our Visual Studio projects and created
I've just introduced a friend to GNU Screen and they're having a hard time
I've just read an article that supposedly introduced me to a new concept: Up
I was just made aware of a bug I introduced, the thing that surprised
As far as I'm concerned, the ideal amount of threads is 3: one for
I just introduced Spring in a JSF web application, and now I'm feeling tempted

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.