Flame has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to figure out how to use threads, and I'm getting rather confused.

I'm trying to execute the following code on ActivePerl (the installer SAID it supported threads but I get this error: No threads in this perl at thread.pl line 4.)

use Thread; $huh = Thread->new(\&thread1); for(1..200){ print "This is the main program"; } print "Waiting for thread now"; $stuff = $huh->join(); sub thread1 { for(1..200){ print "Thread1 is running!"; } return 1; }


It was suppost to be a simple experement that I could build off of.

Any help would be appreciated (heres a tutorial suggestion... threads in perl!)


"Wierd things happen, get used to it"

Flame ~ Lead Programmer: GMS
http://gms.uoe.org

Replies are listed 'Best First'.
(mitd)Re: Thread
by mitd (Curate) on Sep 25, 2001 at 06:05 UTC
    excerpt from Thread.pm doc:
    CAVEAT

    The Thread extension requires Perl to be built in a particular way to enable the older 5.005 threading model. Just to confuse matters, there is an alternate threading model known as "ithreads" that does NOT support this extension. If you are using a binary distribution such as ActivePerl that is built with ithreads support, this extension CANNOT be used.

    You got some version etc checking to do, try perl -V

    mitd-Made in the Dark
    'My favourite colour appears to be grey.'

      I've done a perl -v and this is what I got... I have no idea what some of it means.... but I thought the words 'multi-thread' meant it could use Thread.pm

      If I'm wrong... then what does it mean???

      This is perl, v5.6.0 built for MSWin32-x86-multi-thread (with 1 registered patch, see perl -V for more detail) Copyright 1987-2000, Larry Wall Binary build 623 provided by ActiveState Tool Corp. http://www.ActiveS +tate.com Built 16:27:07 Dec 15 2000 Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5.0 source +kit. Complete documentation for Perl, including FAQ lists, should be found +on this system using `man perl' or `perldoc perl'. If you have access to + the Internet, point your browser at http://www.perl.com/, the Perl Home Pa +ge.


      I then did a perl -V and got this

      Summary of my perl5 (revision 5 version 6 subversion 0) configuration: Platform: osname=MSWin32, osvers=4.0, archname=MSWin32-x86-multi-thread uname='' config_args='undef' hint=recommended, useposix=true, d_sigaction=undef usethreads=undef use5005threads=undef useithreads=define usemultip +licity=def ine useperlio=undef d_sfio=undef uselargefiles=undef use64bitint=undef use64bitall=undef uselongdouble=undef usesocks=u +ndef Compiler: cc='cl', optimize='-O1 -MD -DNDEBUG', gccversion= cppflags='-DWIN32' ccflags ='-O1 -MD -DNDEBUG -DWIN32 -D_CONSOLE -DNO_STRICT -DHAVE_D +ES_FCRYPT -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -DPERL_MSVCRT_READFIX' stdchar='char', d_stdstdio=define, usevfork=false intsize=4, longsize=4, ptrsize=4, doublesize=8 d_longlong=undef, longlongsize=8, d_longdbl=define, longdblsize=10 ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', + lseeksize =4 alignbytes=8, usemymalloc=n, prototype=define Linker and Libraries: ld='link', ldflags ='-nologo -nodefaultlib -release -libpath:"E:\ +Perl\lib\C ORE" -machine:x86' libpth="E:\Perl\lib\CORE" libs= oldnames.lib kernel32.lib user32.lib gdi32.lib winspool.lib + comdlg32 .lib advapi32.lib shell32.lib ole32.lib oleaut32.lib netapi32.lib uui +d.lib wsoc k32.lib mpr.lib winmm.lib version.lib odbc32.lib odbccp32.lib msvcrt. +lib libc=msvcrt.lib, so=dll, useshrplib=yes, libperl=perl56.lib Dynamic Linking: dlsrc=dl_win32.xs, dlext=dll, d_dlsymun=undef, ccdlflags=' ' cccdlflags=' ', lddlflags='-dll -nologo -nodefaultlib -release -l +ibpath:"E: \Perl\lib\CORE" -machine:x86' Characteristics of this binary (from libperl): Compile-time options: MULTIPLICITY USE_ITHREADS PERL_IMPLICIT_CONTEX +T PERL_IMP LICIT_SYS Locally applied patches: ActivePerl Build 623 Built under MSWin32 Compiled at Dec 15 2000 16:27:07 @INC: E:/Perl/lib E:/Perl/site/lib .



      "Wierd things happen, get used to it"

      Flame ~ Lead Programmer: GMS
      http://gms.uoe.org
        Your info is on perl -V (uc V) line begining with:
        usethreads= ...

        You have ithreads complied in.

        mitd-Made in the Dark
        'My favourite colour appears to be grey.

Re (tilly) 1: Thread
by tilly (Archbishop) on Sep 25, 2001 at 07:40 UTC
    Perl's threading model is experimental and unsafe. Unless this is just for personal amusement, and you are willing to accept random crashes for no good reason, I would suggest not using Perl's threading model.

    If you want to use threading and Perl, it is possible, but the safe way to do it will involve some C programming. See Back to the Future of Threads (Part II) for details.