develooper Front page | perl.perl5.porters | Postings from August 2009

Restrictions on loading 'threads'

Jerry D. Hedden
August 10, 2009 05:54
Restrictions on loading 'threads'
Message ID: contains the following statement:

    It is also important to note that you must enable threads by
    doing C<use threads> as early as possible in the script, and
    that it is not possible to enable threading inside an
    C<eval "">, C<do>, C<require>, or C<use>.

Thinking about it, I don't believe the latter part of that sentence is true
anymore.  For example, the following works just fine on both threaded and
non-threaded Perls:


    use strict;
    use warnings;

    my $i_am_threaded = eval 'use threads; 1';

        if ($i_am_threaded) {
            threads->create(sub {
                    print("This is a thread speaking.\n");
        } else {
            print("Can't use threads.\n");

    # EOF

While the first part of the statement is definitely good advice, should the
latter part be removed, or can anyone recommend some more appropriate wording? Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About