5

I am looking to run two subroutines in parallel, where both perform the same task on an android handset using ADB commands. With help from SO and other research I have produced the following code below, however I am new to multithreading and I get an error of 'Free to wrong pool' during execution. I am assuming I get this as I am using the $_ variable in both threads, is this correct? I am using Windows7 to run this, but my Perl interpreter crashes on running this script. Any guidance would be greatly appreciated. Thanks.

use strict;
use Win32::OLE;
use EFS_Handle;
use HelperFunctions;
use threads;

#### Various ADB command sequences follow ####
#### Start of multithread to run the same function on different android handsets ####

my @jobs;

push @jobs, threads->create(
    sub {
        print "\n\t" . curTime() . " :\t DUT Time at start of MPLMN search";

        open my $fh1, '>', "output.txt" or die "Cannot open output.txt: $!";
        my $pid1 = open my $log1, "-|", "adb -s 42d8d7dd logcat";

        system('adb -s 42d8d7dd shell input keyevent KEYCODE_ENTER');

        while (<$log1>) {
            $fh1->print($_);
            last if m/Sorted scan results/;
        }
        kill "TERM", $pid1;
        close $log1;
        print "\n\t" . curTime() . " :\t DUT Time at End of MPLMN search\n";
    }
);

push @jobs, threads->create(
    sub {
        print "\n\t" . curTime() . " :\t REF Time at start of MPLMN search";

        open my $fh, '>', "output.txt" or die "Cannot open output.txt: $!";
        my $pid = open my $log, "-|", "adb -s 0123456789ABCDEF logcat";

        system('adb -s 0123456789ABCDEF shell input keyevent KEYCODE_ENTER');

        while (<$log>) {
            $fh->print($_);
            last if m/EVENT_NETWORK_SCAN_COMPLETED/;
        }
        kill "TERM", $pid;
        close $log;
        print "\n\t" . curTime() . " :\t REF Time at End of MPLMN search\n";

    }
);

$_->join for @jobs;
8
  • can you provide exact error with line number? side note; parametrize your function as they are 99% similar Commented Dec 2, 2013 at 9:56
  • Hi, thanks for looking into this. The error I get is this >>> Free to wrong pool 2a3fec0 not 5a8790 on line >>> while(<$log1>) { <<< In the first thread. Commented Dec 2, 2013 at 10:31
  • Try upgrading your version of Perl. Commented Dec 2, 2013 at 13:50
  • Hi ikegami, I'm using Strawberry Perl version 5.18.1, so I was hoping it would be reasonably up-to-date. Commented Dec 2, 2013 at 14:07
  • 2
    Why not use fork? the threading of perl isn't that good / stable. The error you are seeing is caused due to (a) shared object being mishandled. You aren't actually sharing data between the two threads, so just do a fork() Commented Dec 2, 2013 at 20:28

3 Answers 3

1

The Win32::OLE module is famously not thread-safe

If you remove use Win32::OLE (you don't seem to use it) then your code will run fine

If have my doubts about adb cooperating with multiple simultaneous commands, but that is a different matter

Sign up to request clarification or add additional context in comments.

Comments

0

I think the problem could be related to the fact you are writing from both threads to the same file "output.txt" with ">". Try opening them with ">>".

Also remember to close that file.

1 Comment

This is not an answer. It should be a posted as a comment
0

"I am assuming I get this as I am using the $_ variable in both threads..."

If you use strict, and $_ is in separate methods/subroutines, then there should be no global-access problems.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.