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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:36:45+00:00 2026-05-28T20:36:45+00:00

If there are explicit examples in another post please let me know. I am

  • 0

If there are explicit examples in another post please let me know. I am having problems with interleaved printing from my threads. I am trying to control my threads by using a shared variable across all threads. The pseudo code below highlights the pieces of my code giving me problems. I have tried everything to make the threads wait their turns to print. Right now only a few output lines are being destroyed.

#!/usr/bin/perl                                                                                                                                
use threads;
use threads::shared;

my $PRINTFLAG :shared = 1;

Run_Threads();

sub Do_stuff{

    lock($PRINTFLAG);
    cond_wait($PRINTFLAG) until $PRINTFLAG == 1;
    $PRINTFLAG = 0;
    print "$working\n";
    $PRINTFLAG =1;
}

Sub to spawn threads.

sub Run_Threads{

    my @threads;

    for (my $i = 1; $i <= 5; $i++){
        push @threads, threads->create(\&Do_stuff);
    }

    foreach (@threads){
        $_->join;
    }
}
  • 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-28T20:36:45+00:00Added an answer on May 28, 2026 at 8:36 pm

    It would seem that each thread has its own handle, and thus its own output buffer. Considering that Perl file handles can’t be shared using the mechanisms in threads::shared, that’s not very surprising.

    That means you need to flush the handle’s buffer before releasing the lock. You can do that explicitly:

    select->flush();       # Flush handle currently default for print.
    

    Or you can have perl flush automatically after every print to that handle:

    select->autoflush(1);  # Autoflush handle currently default for print.
    $| = 1;                # Autoflush handle currently default for print.
    

    Note: To use the ->flush and ->autoflush methods (but not for $|=1;) before Perl 5.14, you’ll need to also load IO::Handle.


    By the way,

    my $PRINTFLAG :shared = 1;
    lock($PRINTFLAG);
    cond_wait($PRINTFLAG) until $PRINTFLAG == 1;
    $PRINTFLAG = 0;
    print "$d\n";
    $PRINTFLAG =1;
    

    can be simplified to

    my $PRINTMUTEX :shared;
    lock($PRINTMUTEX);
    print "$d\n";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Many examples out there advocate explicit rollback of database transactions, along the lines of:
Is there any efficiency difference in an explicit vs implicit inner join? For example:
Is there explicit support for Single Table Inheritance in Django? Last I heard, the
Does Delphi call inherited on overridden procedures if there is no explicit call in
I'm trying to update a cell in jqgrid permanently upon loading. I know I
I'm having troubles choosing the best solution to my problems. Currently, I'm working on
Summary Let's say I have two C# 4.0 classes, one inheriting from the other:
Coming from a web background, where explicit sizing is generally not considered best practice,
Are there any scenarios where it is absolutely necessary to perform an explicit cast
I have a few questions about the linkage from the following variables. By examples

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.