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 7403249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:00:56+00:00 2026-05-29T05:00:56+00:00

For the below program I’m getting this error message: Thread 2 terminated abnormally: Invalid

  • 0

For the below program I’m getting this error message:

Thread 2 terminated abnormally: Invalid value for shared scalar at
reader Foo::bar (defined at … line 9) line 10.

The program consists of a pipeline where the first thread creates some Moose-based objects and puts them in the queue, which are then picked up in the second thread. The problem seems to be that the attribute is lazy because the error disappears if I remove the lazy setting.

package Foo;
use Moose;

has 'bar' => (
    is      => 'ro',
    isa     => 'HashRef', # the error doesn't happen with simpler datatypes
    lazy    => 1, # this line causes the error
    default => sub { return { map {$_ => $_} (1 .. 10) } },
);

package main;

use threads;
use Thread::Queue;

my $threadq = Thread::Queue->new;

sub create {
    # $_ doesn't seem to be thread-safe
    # this resolved another problem I had with a custom Moose type constraint 
    # where the 'where' clause used $_
    local $_;

    $threadq->enqueue( Foo->new ) foreach 1 .. 5;
    $threadq->enqueue( undef );
    return;
}

sub process {
    local $_;
    while (my $f = $threadq->dequeue) {
        print keys %{$f->bar}, "\n";
    }
    return;
}

threads->create( \&create )->join;
threads->create( \&process )->join;

Can anyone shed some light on this problem? Is Moose itself thread-safe (I couldn’t find much in the documentation about this)?

  • 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-29T05:00:58+00:00Added an answer on May 29, 2026 at 5:00 am

    Thread::Queue replaces all the hashes and arrays in your object with shared hashes and arrays with the same content, and does so recursively.

    At the same time, you are attempting to change the object. Yeah, that’s not gonna end well. (And not because of any bug in Moose, Thread::Queue or threads.)

    A solution is to pass around the object in a serialised form. You could handle serialisation and deserialisation yourself, or you could use Thread::Queue::Any to have it done implicitly.

    use threads;
    use Thread::Queue::Any;
    
    my $threadq = Thread::Queue::Any->new;
    
    sub create {
        $threadq->enqueue( Foo->new ) for 1 .. 5;
        $threadq->enqueue( );
    }
    
    sub process {
        while ( my ($f) = $threadq->dequeue ) {
           print sort keys %{$f->bar};
           print "\n";
        }
    }
    

    Note there are subtle but important differences in the usage of enqueue and dequeue. Most importantly, dequeue must be called in list context when using T::Q::A. This is because enqueue‘s argument list is passed as one message, and dequeue returns that argument list.

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

Sidebar

Related Questions

Question: The below program throws a runtime error (invalid conversion from double) because there
I am getting an error for the below program on the data_derived.m data_derived.m @implementation
In this below program i am printing the contents of the div when i
In below program i am getting output as false .But as per my understanding
Consider the below program myThread = new Thread( new ThreadStart( delegate { Method1(); Method2();
For the program below i get the following error- Potential leak of an object
When I try the below program its gives me the :error C2064: I tried
I'm obviously not quite getting the 'end-of-file' concept with C++ as the below program
consider the program below char str[5]; strcpy(str,Hello12345678); printf(%s,str); When run this program gives segmentation
I have a C program below written on UNIX. I am getting segmentation fault.

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.