in reply to Compiling with pp on Strawberry Perl on Windows with threads fails

Your child process management logic is flawed.

Try this (slightly improved, but simplistic) code:

use strict; use warnings; #Hash of Child PIDs our %ChildPids; #my $MaxThreadsAtOneTime = 5; my $MaxThreadsAtOneTime = 2; #my $MaxThreadsAtOneTime = 1; my $ThreadsToRun = 10; #### MAIN # Submit first group of children print("Submitting first batch of children\n"); my $ThreadNum = 0; my $activeThreads = 0; while ($ThreadNum < $ThreadsToRun) { $ThreadNum++; my $ChildPid; if ($activeThreads >= $MaxThreadsAtOneTime){ print "Waiting for a thread to finish\n"; for (keys %ChildPids){ my $rslt = waitpid( $_,0 ); print "Waiting pid $_ = ", $rslt , "\n"; delete $ChildPids{$_}; # So we dont wait again.. last; } $activeThreads --; } if ($ChildPid = fork) { # parent code # Collect pid print("Parent Created Child $ThreadNum with pid $ChildPid\n"); $ChildPids{$ChildPid} = $ThreadNum; $activeThreads++; } elsif (defined $ChildPid) { # child code print(" Child pid $$ checking in as thread $ThreadNum\n"); my $SleepTime = int(rand(10)); print(" Child pid $$ sleeping for $SleepTime seconds\n"); sleep $SleepTime; print(" Child pid $$ exiting\n"); exit(); } } print "Parent Code done after ThreadNum=$ThreadNum .. Waiting\n"; for (keys %ChildPids){ print "Waiting pid $_ = ", waitpid( $_,0 ) , "\n"; } print "Exiting Parent\n";

        There is no time like the present for postponing what you ought to be doing.

  • Comment on Re: Compiling with pp on Strawberry Perl on Windows with threads fails
  • Download Code

Replies are listed 'Best First'.
Re^2: Compiling with pp on Strawberry Perl on Windows with threads fails
by rminsko (Initiate) on Jul 08, 2016 at 17:17 UTC

    I tried your version, and it does work running just the perl on Windows 7 with Strawberry Perl

    This is perl 5, version 24, subversion 0 (v5.24.0) built for MSWin32-x64-multi-thread

    Running with pp also works to create the exe file

    pp -x ThreadTest2.pl -o ThreadTest2.exe

    But when running the created executable, it gets the same popup error as my version.

    ThreadTest2.exe has stopped working

    I've been told by other co-workers that this "behavior" is also seen with pp on ActiveState perl and with Perl2Exe compiler, though I haven't tested those. Still scratching my head.

      This sample code shows the author using a "select" to "sleep", after invoking "fork". Other Internet postings also indicate the need for sleep after fork, on Windows.

      Could you try adding a "sleep 1;" after the fork, to see if that helps ? (I am having trouble installing a working PAR::Packer).

              There is no time like the present for postponing what you ought to be doing.

        I have the same problem, fork() works perfectly fine until you compile the code with pp and run the executable. I've added a sleep() as suggested above, but it did help.