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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:28:19+00:00 2026-06-03T11:28:19+00:00

I need help with multithreading in Perl. The basic logic is to initiate 20

  • 0

I need help with multithreading in Perl.

The basic logic is to initiate 20 threads. I have one array @dataarray and I want 20 chunks of data to be passed to each thread. Say, @dataarray has 200 rows of data in it, so first 10 rows will be going to thread 1, next 10 should be sent to thread 2, so they don’t overwrite each others data and ultimately after processing thread should update return result to @outputarray at the same index position as of source @datarray.

For example: row 19(index position 18) from @dataarray was sent to thread number 2 so after processing it thread 2 should update $outputarray[18] = $processed_string.

Just need to figure out how to send from and to positions of array to a particular thread.

#!/usr/bin/perl

use strict;
use threads;
my $num_of_threads = 20;
my @threads = initThreads();
my @dataarray;

foreach(@threads)
    {
    $_ = threads->create(\&doOperation);
    }

foreach(@threads)
     {
     $_->join();
     }

sub initThreads
     {
       my @initThreads;
         for(my $i = 1;$i<=$num_of_threads;$i++)
         {
         push(@initThreads,$i);
     }
     return @initThreads;
     }

sub doOperation
    {
    # Get the thread id. Allows each thread to be identified.
    my $id = threads->tid();
    # Process something--- on array chunk
    print "Thread $id done!\n";
    # Exit the thread
    threads->exit();
    }
  • 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-06-03T11:28:23+00:00Added an answer on June 3, 2026 at 11:28 am

    I don’t think what I said earlier about having to use threads::shared is correct. I had to go check the documentation, and I am not sure. I am never sure when it comes to threads with Perl.

    Update: As it turned out, my incomplete understanding was revealed again. At the very least, you need threads::shared to be able to put the results in @output from within each thread.

    #!/usr/bin/env perl
    
    use strict; use warnings;
    use threads;
    use threads::shared;
    
    use List::Util qw( sum );
    use YAML;
    
    use constant NUM_THREADS => 20;
    
    my @output :shared;
    my @data = ( ([1 .. 10]) x 200);
    
    # note that you'll need different logic to handle left over
    # chunks if @data is not evenly divisible by NUM_THREADS
    my $chunk_size = @data / NUM_THREADS;
    
    my @threads;
    
    for my $chunk ( 1 .. NUM_THREADS ) {
        my $start = ($chunk - 1) * $chunk_size;
        push @threads, threads->create(
            \&doOperation,
            \@data,
            $start,
            ($start + $chunk_size - 1),
            \@output,
        );
    }
    
    $_->join for @threads;
    
    print Dump \@output;
    
    sub doOperation{
        my ($data, $start, $end, $output) = @_;
    
        my $id = threads->tid;
    
        print "Thread [$id] starting\n";
    
        for my $i ($start .. $end) {
            print "Thread [$id] processing row $i\n";
            $output->[$i] = sum @{ $data->[$i] };
            sleep 1 if 0.2 > rand;
        }
    
        print "Thread $id done!\n";
    
        return;
    }
    

    Output:

    - 55
    - 55
    - 55
    …
    - 55
    - 55
    - 55
    - 55
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need help... I have 3 classes, Manager which holds 2 pointers. One to class
Need help writing a script downloads data from google insight using c# this is
need help/guide for sql select query, I have 2 table stock and stock_history, in
need help regarding USSD Gateway. I have to develop an app, which will directly
need help in error in database pivot. i have table tamed table_score like below:
Hey folks, every once in a while I have the need to automate data
Need help. I have Graph API code that uses an auth token with offline
Need help for that. I have a tabbar with 2 views, A and B
Need help in handling data and populating: I am creating Employee database, there are
Need help with copying array object to a temp array object using a for

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.