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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:11:39+00:00 2026-05-17T18:11:39+00:00

I think this might be a fairly easy question. I found a lot of

  • 0

I think this might be a fairly easy question.

I found a lot of examples using threads and shared variables but in no example a shared variable was created inside a thread. I want to make sure I don’t do something that seems to work and will break some time in the future.

The reason I need this is I have a shared hash that maps keys to array refs. Those refs are created/filled by one thread and read/modified by another (proper synchronization is assumed). In order to store those array refs I have to make them shared too. Otherwise I get the error Invalid value for shared scalar.

Following is an example:

my %hash :shared;

my $t1 = threads->create(
    sub { my @ar :shared = (1,2,3); $hash{foo} = \@ar });
$t1->join;

my $t2 = threads->create(
    sub { print Dumper(\%hash) });
$t2->join;

This works as expected: The second thread sees the changes the first made. But does this really hold under all circumstances?


Some clarifications (regarding Ian’s answer):

I have one thread A reading from a pipe and waiting for input. If there is any, thread A will write this input in a shared hash (it maps scalars to hashes… those are the hashes that need to be declared shared as well) and continues to listen on the pipe. Another thread B gets notified (via cond_wait/cond_signal) when there is something to do, works on the stuff in the shared hash and deletes the appropriate entries upon completion. Meanwhile A can add new stuff to the hash.

So regarding Ian’s question

[…] Hence most people create all their shared variables before starting any sub-threads.

Therefore even if shared variables can be created in a thread, how useful would it be?

The shared hash is a dynamically growing and shrinking data structure that represents scheduled work that hasn’t yet been worked on. Therefore it makes no sense to create the complete data structure at the start of the program.

Also the program has to be in (at least) two threads because reading from the pipe blocks of course. Furthermore I don’t see any way to make this happen without sharing variables.

  • 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-17T18:11:40+00:00Added an answer on May 17, 2026 at 6:11 pm

    I don’t feel good answering my own question but I think the answers so far don’t really answer it. If something better comes along, I’d be happy to accept that. Eric’s answer helped though.

    I now think there is no problem with sharing variables inside threads. The reasoning is: Threads::Queue‘s enqueue() method shares anthing it enqueues. It does so with shared_clone. Since enqueuing should be good from any thread, sharing should too.

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

Sidebar

Related Questions

I mostly develop using C#, but I think this question might be suitable for
I think this could be a very easy question for you. But I have
I think this might be a problem with the theme I'm using ( Nitobe
Firstly, This might seem like a long question. I don't think it is... The
You might think that this question is already answered. However, I couldn't find the
This might seem a bit like a do-my-homework-for-me question (and it is), but I
This might be something very straight forward and I really think it should work
I think my code might be written incorrectly and this is causing my problem.
I really nead to ask you this one, many of you might think it's
I'm fairly new to jQuery, and think I might be making something harder than

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.