develooper Front page | perl.ithreads | Postings from August 2009

Restrictions on loading 'threads'

From:
Jerry D. Hedden
Date:
August 10, 2009 05:54
Subject:
Restrictions on loading 'threads'
Message ID:
1ff86f510908100553w27883b3ev785407d62ce349e6@mail.gmail.com
threads.pm 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:

    #!/usr/bin/perl

    use strict;
    use warnings;

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

    MAIN:
    {
        if ($i_am_threaded) {
            threads->create(sub {
                    print("This is a thread speaking.\n");
                })->join();
        } 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?



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About