Why am I getting a segmentation fault when I use binmode with threads in Perl?
this call
my $th = threads->create(\&print, "Hello thread World!\n");
$th->join();
works fine. But as soon as I add
binmode(STDOUT, ":encoding(ISO-8859-1)");
to my script file, I get an error like "segmentation fault", "access denied".
What is wrong to define an encoding type when trying to call a perl thread?
开发者_开发技巧Example:
use strict; use warnings;
use threads;
binmode(STDOUT, ":encoding(ISO-8859-1)");
my $th = threads->create(\&print, "Hello thread World!\n");
$th->join();
sub print {
print @_;
}
This code does not work for me.
Kind Regards
--Andy
This was reported as bug in Perl's bug tracker. I have same failure on 5.12 RC0 on Windows.
First, note that having a subroutine print
with the same name as a built-in function is likely to cause a lot of confusion (if not to perl
definitely to you or anyone who needs to read your code).
Second, I do not observe the problem with:
#!/usr/bin/perl
use strict; use warnings;
use threads;
my $th = threads->create(\&print, "Hello thread World!\n");
$th->join();
sub print {
binmode(STDOUT, ":encoding(ISO-8859-1)");
print @_;
}
Output:
C:\Temp> t Hello thread World!
However, since STDOUT
is a package variable, I would not recommend doing anything like this.
You should post a short but complete script that exhibits the problem rather than bits and pieces and also describe what you are trying to do rather than just the mechanical steps.
#!/usr/bin/perl
use strict; use warnings; use threads;
open my $fh, '>>', '/tmp/1' or die $!;
binmode $fh, ':encoding(isolatin1)' or die $!; # LINE 'A'
my $t = threads->create(sub { sleep 1; }); # LINE 'B'
$t->join();
The above segfaults in Perl 5.12.4 in line 'B'. If you swap lines 'A' and 'B', the code runs fine. It also runs fine if you close $fh before you create the thread. So until this issue is resolved, just make sure that you do not have any file handles open for which you have binmoded encodings, when you create a new thread.
精彩评论