Artur 'sky' Bergman - Perl Threads ------- Thread instruction, address & data several perl interp in 1 process Perl defaults, to no shared data Clone runing interp, close to fork but no os level \\\ # -Dusereentrant for OSX threads are platform independant USE use threads; # pragma & module threads->create(sub{}) ->new ->join #waits for finish , returns val, destroys, ->detach # waits till term , quits, returns NO val, problem if not finished by program term, BAD Each thread about 1 meg, with no data No security seg faults if detached does not threads->create self # current list # all non joined non detached tid # thread if ############################## Might, you don't know Never assume a thread will wait # init all data before thread threads sleep when ever for whatever ammount of time no order, cpu os etc #shared use threads ::shared my $foo :shared =0; share (); my $ref = &shared({}); ### need amperstand if annoy ref NO SHARED FILEHANDLEs does not work in code, packages or IO can't use on tied vars, is a tied vars DESTROY called on each thread async ($object) ## used for smale async ($objectl; 0 ) ## returns 0 not object (objects would be GCd in both threads and main) threads::shared:: #scalars 4x slower , 10-20x on list i++ === temp = $i i = $temp + 1; lock(); ##scope based, advisory as advisory, use in all location lock is lexical, us scope to unlock deadlocks ### cond_signal cond_wait ($i)until($i); cond_broadcast wait waits till signal, on locked var #### multipplicty comipliation to perl perl comipled for threading 10% slower Eventloops POE Event.om GTK etc Coroutines fork+shared mem #threads multi CPU used Doesn't lock on file OS # this is it Portable Easy #VS forks not portable ugly ## slightly adged unix threads SUCK # Eventloops hard to think of race cond in all alternatives ## IO Socket , threads Demo of Echo server rocks, very short very clean to clean up ## :shared http://search.cpan.org/author/ABERGMAN/Attribute-Handlers-0.78/lib/Attribute/Handlers.pm :method :unique :lval ## No Portable Thread KILL Most Modules create no threads, most just work All pure perl Modules work, XS, FILEs, sockets are problems DBI works, if DBD fail prob lib issues, (sockets etc, usually work) test::more is thread freindly ##### Thread::Queue enque deque dequeue_nb pending #### Thread Semaphore #### Thread signal Linux usage Safe Signals, map signals between, different threads ###### Elizibet Mattisen Thread::tie --> tie anyway use --> load module in 1 thread need --> modules status --> what is thread doing rand --> rand numbwe Deadlock --> detects pool --> tasks to thread groups ##### my_cxt helps with XS modules threads modules helps with 'magic', dup get free XS Mutexs 5.8.1 fixes a lot of bugs, mainly memory leaks , one deadlocks, $0, #future TPF Grant -FAQ -Modules -POE is starting to use -PerlInterpreter.pm -COW--copy on write #Apache2 -Multiple Multi Process Modules fork thread Pool of threads with mod_perl2 a pool of interps perl threads, the threads are not linked to OS threads. No need to proxies, 'cause perl handles RT 3 on win32 Apache mod_perl (Crazy Taiwanese Perl Hackers) -mod_perl2 -- threads using