Minutes of Meeting OCT 19 2006

November 26th, 2006 by SimonAW

Attendees

  • Bruce
  • Eliot
  • Steven
  • Rob
  • Chris
  • et al

Chris Introduction to Threads in Perl

Models

  • Boss/Worker
  • Work Crew
  • Pipeline

What kind of threads are Perl Threads

Perl Threads are specific to Perl. There are similarities between perl’s
threading system and others. Perl is not POSIX threads. There are
no mutexes or thread priorities and data is not shared between threads by default.

Creating Threads

Threads can be created via the threads module. When compiling perl from source it is important to tell Configure that you wish to use threads. The threading module is known as threads.


1 #!/usr/bin/perl
2 use threads
3 my $thr = threads->new(\&sub1);
4 sub sub1 { print "In thread\n"; 1;}
5 $thr->join;

Line 2
We create a thread and sub1 is executed as the thread.
Line 5
The parent thread waits for the child to return. Join will wait for a thread to complete and will retrieve the thread’s return value; In line 4 we returned ‘1′. join() will wait for that routine to finish and receive the return value;

Threads and data

The biggest difference between Perl’s threads and other threading
systems is that by default no data is shared. When a thread is created
there is a similar feel to what happens during a fork(). All data is
copied to the thread. In reality this is quite expensive in terms of
memory and cpu usage. To make threads useful one should share some
data. The module threads::shared is used to allow threads to share
data.

Some examples of sharing data:


1. #!/usr/bin/perl
2. use threads;
3. use threads::shared
4. my $foo : shared = 1;
5. my $bar = 1;
6. threads->new(sub { $foo++; $bar++})->join();
7. print "\$foo = $foo\n";
8. print "\$bar = $bar\n";

Line 4
We create a variable and mark is as shared. We then initialize
that variable to 1. Anything in a program you can share a variable.
poker tour giocogiochi online poker gratis7 card stud inlineaaces texas holdem multiplayerholdem poker online,poker texas holdem online,poker texas holdem on linesexy poker gratispoker tournament 2007scommesse internetnoble pokerpoker online italiapoker milanovideo poker gratispoker italiadownload video poker gratispoker pctexas holdem poker onlinetornei poker gratistornei texas holdembonus senza deposito pokerplay omaha poker freeregole omaharegole poker omahapoker holdem gratissuper pokeristruzioni gioco pokerplay 7 card stud onlinestrip poker gamescaricare gratis pokerhollywood pokerholdem poker,poker holdem gratis,holdem poker italiastreap poker on lineonline poker tournamentscarica gioco pokeronline poker roomtrucchi casino onlinecasino games proslots machines gratisgiochi casino onlineroulette russafree kenovideo poker online gratisbonus dei casinocasino italia bonusbetandwin casinoprobabilit? roulettegiochi casino online gratiscasino sanremo onlinegioco baccarat in lineavirtual casinoamerican roulette It is important to know that when you mark it as shared any values
in that variable are destroyed.
Line 5
We create a bar variable and do not share it.
Line 6
We create a thread and wait for it to finish. The thread simply
increments our two variables.
Line 7
Since we shared foo and the thread incremented it the value is
2.
Line 8
Even though the thread incremented bar we did not share it.
Value is 1.

Types of values that can be shared

Array’s, Hashes, and values can be shared. For an array and hash the
children are shared. There is an exception for the hash that only one
level deep will be shared. If you have a hash that has multiple levels you’ll
need to share each level of that hash. Only simple values or references to shared
data is allowed.


--- listing 4.2 ---
1.#!/usr/bin/perl
2.use threads;
3.use threads::shared
4.my %hash : shared = ();
5.$hash{level1} = &share({})

Line 5. We want hash named “level1″ to be shared between threads to we
mark it as shared.
Line 6. We assign the first value in the “level1″ hash. Note that we marked
it as shared before we assigned a value.

Locking

It is important that when multiple threads access the same data that the data is
protected. Perl can place a lock on a variable and a thread will block until
that lock has been released. There is no unlock method. The lock stays in effect until
it is out of scope. If a thread tries to lock the same variable within the scope of a
previous lock then it will be ignored.

--- Listing 5.1 ---
1. #!/usr/bin/perl
2. use threads;
3. use threads::shared;
4. my $LCK : shared;
5. my $foo : shared = 1;
6. my $thr = thread->new(sub {
7. lock($LCK);
8. $foo++; 1;});
9. { lock($LCK); print "\$foo = $foo\n"; }
10. $thr->join()

Line 4
We create a shared variable to use as locking. You can lock any variable
but to keep things clear I usually create one variable just to use for locking.
Line 5
We create a shared variable that we’ll protect using the lock
Line 6
New thread created
Line 7
Lock the variable. Note that the thread will block on this line until
the lock is avaialble.
Line 8
Simply increment $foo by one and return
Line 9
We lock our variable and print $foo. Brackets are used to illustrate the
concept of the lock being actinve within the scope of the lock. Once we
are out of scope variable is automatically unlocked.

Meeting NOV 16 2006 — Live –

November 16th, 2006 by SimonAW

Atlanta.pm irc channel

November 16th, 2006 by mdw

Minutes of Meeting SEP 21 2006

September 22nd, 2006 by SimonAW

Minutes of Meeting AUG 17 2006

August 20th, 2006 by SimonAW

First draft of a flyer to post for our meetings

August 6th, 2006 by Russ Thomas

Minutes of Meeting JUL 20 2006

July 31st, 2006 by SimonAW

Any artistic folks ready to create a logo for the blog?

July 21st, 2006 by mdw