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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:01:36+00:00 2026-06-03T15:01:36+00:00

My Perl script needs to run multiple threads simultaneously… use threads (‘yield’, ‘exit’ =>

  • 0

My Perl script needs to run multiple threads simultaneously…

use threads ('yield', 'exit' => 'threads_only');
use threads::shared;
use strict;
use warnings;
 no warnings 'threads';
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Async;
use ...

…and such threads need to obtain some information from web, so HTTP::Async is used.

my $request = HTTP::Request->new;
   $request->protocol('HTTP/1.1');
   $request->method('GET');
   $request->header('User-Agent' => '...');

my $async = HTTP::Async->new( slots            => 100,
                              timeout          => REQUEST_TIMEOUT,
                              max_request_time => REQUEST_TIMEOUT );

But some threads need to access web only when other thread(s) says so.

my $start = [Time::HiRes::gettimeofday()];
my @threads = ();
foreach ... {
  $thread = threads->create(
    sub {
           local $SIG{KILL} = sub { threads->exit };
           my $url = shift;
           if ($url ... ) {
             # wait for "go" signal from other threads
           }
           my ($response, $data);
           $request->url($url);
           $data = '';
           $async->add($request);
           while ($response = $async->wait_for_next_response) {
             threads->yield();
             $data .= $response->as_string;
           }
           if ($data ... ) {
             # send "go" signal to waiting threads
           }
         }
       }, $_);

  if (defined $thread) {
    $thread->detach;
    push (@threads, $thread);
  }
}

There might be one or more threads waiting for “go” signal and there might be one or more threads that such “go” signal can send. At the beginning the status of semaphore is “wait” and once it turns to “go“, it will stay so.

Finally, app checks max running time. If threads are running too long, self-termination signal is sent.

my $running;
do {
  $running = 0;
  foreach my $thread (@threads) {
    $running++ if $thread->is_running();
  }
  threads->yield();
} until (($running == 0) || 
         (Time::HiRes::tv_interval($start) > MAX_RUN_TIME));
$running = 0;
foreach my $thread (@threads) {
  if ($thread->is_running()) {
    $thread->kill('KILL');
    $running++;
  }
}
threads->yield();

Now to the point. My questions are:

  1. How can I most effectively code waiting “semaphore” in the script (see comments in script above). Should I simply use just shared variable with some dummy sleep loop?

  2. Do I need to add some sleep loop at the end of app to give time to threads for self-destruction?

  • 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-03T15:01:42+00:00Added an answer on June 3, 2026 at 3:01 pm

    You might look at Thread::Queue to perform this work. You could setup a queue that would handle the signaling between the threads waiting for the ‘go’ signal and the threads sending the ‘go’ signal. Here’s a quick mock-up that I haven’t tested:

    ...
    use Thread::Queue;
    ...
    # In main body
    my $q = Thread::Queue->new();
    ...
    $thread = threads->create(
        sub {
               local $SIG{KILL} = sub { threads->exit };
               my $url = shift;
               if ($url ... ) {
                 # wait for "go" signal from other threads
                 my $mesg = $q->dequeue();
                 # you could put in some termination code if the $mesg isn't 'go'
                 if ($mesg ne 'go') { ... }
               }
               ...
               if ($data ... ) {
                 # send "go" signal to waiting threads
                 $q->enqueue('go');
               }
             }
           }, $_);
    ...
    

    The threads that need to wait for a ‘go’ signal will wait on the dequeue method until something enters the queue. Once a message enters the queue one thread and only one thread will grab the message and process it.

    If you wish to stop the threads so that they won’t run, you can insert a stop message to the head of the queue.

    $q->insert(0, 'stop') foreach (@threads);
    

    There are examples in Thread::Queue and threads CPAN distributions that show this in more detail.

    In response to your second question, the answer is, unfortunately, it depends. When you proceed to terminate your threads, what kind of clean up is required for a clean shutdown? What’s the worst case scenario that could occur if the rug was yanked out from beneath the thread? You would want to plan in any time for the clean up to occur. The other option you could do is wait on each thread to actually complete.

    The reason for my comment asking if you could remove the detach call is because this method allows the main thread to exit and not care what was happening to any child threads. Instead, if you remove this call, and add:

    $_->join() foreach threads->list();
    

    to the end of your main block, this will require the main application to wait for each thread to actually complete.

    If you leave the detach method in place, then you will need to sleep at the end of your code if you require your threads to perform any sort of clean-up. When you call detach on a thread, what you are telling Perl is that you don’t care what the thread is doing when your main thread exits. If the main thread exits and there are threads that still running that have been detached, then the program will finish with no warnings. However, if you don’t require any clean-up, and you still call detach, feel free to exit whenever you like.

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

Sidebar

Related Questions

I have a Perl script that needs to run following an annual schedule with
I'm writing a script in Perl that needs to run at the same time
I am working on a script that needs to run a perl script via
Perl script needs to receive ajax request, send a success message back to browser,
I've got a Perl script that needs to execute another Perl script. This second
I'm making a Perl script that needs to read and obtain the values of
I'm writing a shell script that before running needs to check that system Perl
I need to be able to run a commandline command from my perl script
I am trying to run bp_genbank2gff3.pl (bioperl package) from another perl script that gets
I am writing a Perl script that needs to transfer files between computers using

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.