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

  • Home
  • SEARCH
  • 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 7517637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:31:02+00:00 2026-05-30T01:31:02+00:00

I’m trying to accomplish the following: Have a thread that reads data from a

  • 0

I’m trying to accomplish the following:

  1. Have a thread that reads data from a very large file say about
    10GB and push them into the queue. (I do not wish for the queue to
    get very large either)

  2. While the buildQueue thread is pushing data to the queue at the same time have
    about 5 worker threads de-queue and process data.

I’ve made an attempt but my other threads are unreachable because of a continuous loop in my buildQueue thread.

My approach may be totally wrong. Thanks for any help, it’s much appreciated.

Here’s the code for buildQueue:

sub buildQueue {
    print "Enter a file name: ";
    my $dict_path = <STDIN>;
    chomp($dict_path);
    open DICT_FILE, $dict_path or die("Sorry, could not open file!");
    while (1) {
        if (<DICT_FILE>) {
            if ($queue->pending() < 100) {
                 my $query = <DICT_FILE>;
                 chomp($query);
                 $queue->enqueue($query);
                 my $count = $queue->pending();
                 print "Queue Size: $count Query: $query\n";
            }
        }
    }
}

And as I’ve expected when this thread gets executed nothing else after will be executed because this thread will not finish.

my $builder = new Thread(&buildQueue);

Since the builder thread will be running for a long time I never get to create worker threads.

Here’s the entire code:

#!/usr/bin/perl -w
use strict;
use Thread;
use Thread::Queue;


my $queue = new Thread::Queue();
my @threads;

sub buildQueue {
    print "Enter a file name: ";
    my $dict_path = <STDIN>;
    chomp($dict_path);
    open dict_file, $dict_path or die("Sorry, could not open file!");
    while (1) {
        if (<dict_file>) {
            if ($queue->pending() < 100) {
                 my $query = <dict_file>;
                 chomp($query);
                 $queue->enqueue($query);
                 my $count = $queue->pending();
                 print "Queue Size: $count Query: $query\n";
            }
        }
    }
}

sub processor {
    my $query;
    while (1) {
        if ($query = $queue->dequeue) {
            print "$query\n";
        }
    }
}

my $builder = new Thread(&buildQueue);
push @threads, new Thread(&processor) for 1..5;
  • 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-30T01:31:03+00:00Added an answer on May 30, 2026 at 1:31 am

    You’ll need to mark when you want your threads to exit (via either joinor detach ). The fact that you have infinite loops with no last statements to break out of them is also a problem.

    Edit: I also forgot a very important part! Each worker thread will block, waiting for another item to process off of the queue until they get an undef in the queue. Hence why we specifically enqueue undef once for each thread after the queue builder is done.

    Try:

    #!/usr/bin/perl -w
    use strict;
    use threads;
    use Thread::Queue;
    
    
    my $queue = new Thread::Queue();
    our @threads; #Do you really need our instead of my?
    
    sub buildQueue
    {
        print "Enter a file name: ";
        my $dict_path = <STDIN>;
        chomp($dict_path);
    
        #Three-argument open, please!
        open my $dict_file, "<",$dict_path or die("Sorry, could not open file!");
        while(my $query=<$dict_file>)
        {
            chomp($query);
            while(1)
            {   #Wait to see if our queue has < 100 items...
                if ($queue->pending() < 100) 
                {
                    $queue->enqueue($query);
                    print "Queue Size: " . $queue->pending . "\n";
                    last; #This breaks out of the infinite loop
                }
            }
        }
        close($dict_file);
        foreach(1..5)
        {
            $queue->enqueue(undef);
        }
    }
    
    sub processor 
    {
        my $query;
        while ($query = $queue->dequeue) 
        {
            print "Thread " . threads->tid . " got $query\n";
        }
    }
    
    my $builder=threads->create(\&buildQueue);
    push @threads,threads->create(\&process) for 1..5;
    
    #Waiting for our threads to finish.
    $builder->join;
    foreach(@threads)
    {
        $_->join;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I have a reasonable size flat file database of text documents mostly saved in
I am trying to loop through a bunch of documents I have to put

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.