SlideShare ist ein Scribd-Unternehmen logo
1 von 55
Threading and Perl
 Introduction To Perl Threads and
Basics of Concurrent Programming




                    eric.maki for kwpm
                          24/07/2008
Talk Outline


• Thread Basics
• Threads and Shared Memory API
     – For Perl 5.8.3 and above
• Synchronization Mechanisms
     – What’s built in?
     – Generic synchronization primitives
           • What they are and how they are used
           • What/how you can build from Perl’s built-ins
             (or use from CPAN)

kwpm: threading and perl                                    2
What is a Thread?


• Most generally: an execution
  context.
• Threads normally provide a
  mechanism for managing multiple
  execution contexts within the same
  OS process.
• Threads can interact via shared
  memory, rather than via IPC.

kwpm: threading and perl               3
Threads and Languages


• Language approaches to
  Threads:
     – Java: Supported in the JVM, thus it is a
       core language element.
     – pthreads (POSIX Threads): a cross
       language standard used widely with
       C/C++. This is an API, not an
       implementation.
     – Python: Core piece of language,
       implementation is platform dependent.
kwpm: threading and perl                          4
Threads versus Fibres

• The term ‘thread’ normally refers to
  execution contexts created by user
  processes, but scheduled by the
  kernel.
• ‘Fibres’ are execution contexts
  managed entirely in user-space.
     – Advantage here is selection and
       optimization of scheduling and context
       scope.
     – Obviously, also downsides.

kwpm: threading and perl                        5
Perl and Threads: Versions

• Interpreter Threads introduced in
  Perl 5.6, and widely available and
  reasonably stable in Perl 5.8.
• Perl 5.5 had a different threading
  model, which continued to be
  supported up to 5.10.
     – It never progressed beyond
       experimental.
     – I won’t discuss “old style threads”
       further.

kwpm: threading and perl                     6
ithreads basic model

• Unlike most other thread
  implementations, in ithreads global
  variables are non-shared by
  default.
• A new thread actually gets a copy of
  everything.
• This includes the interpreter
  instance.
• Model is based around explicit
  sharing.
kwpm: threading and perl                 7
ithreads basic model (con’t)


• Very basic model
• No notion of thread priority
• Minimal built in synchronization
  mechanisms:
     – No mutexes
     – No semaphores
• Everything based around shared
  locks.

kwpm: threading and perl             8
Implementation


• Employs pthreads where they are
  available (everywhere but Win32, I
  believe)
• pthreads on Linux are treated as
  ‘special’ processes with a shared
  memory space
• Win32 uses windows threading
  model (some impedance mismatch)

kwpm: threading and perl               9
Jumping in


• We’ll jump into the code examples in
  Perl
• Concurrent programming requires a
  different mindset from strictly
  sequential programming




kwpm: threading and perl             10
Create a thread


use threads;
my $thr = threads->create( &entry_point, @args );


• A new thread is created, with a
     – new interpreter, stack, etc.,
     – new copies of all unshared globals.
• Starts by calling entry_point( @args )
• When the sub returns, the thread is
  complete
• No guarantees of ‘first execution’
kwpm: threading and perl                             11
Thread Death


my $res = $thr->join();


• Return value of a thread is the return
  value of the function
• When a thread completes it waits for
  the result to be read
• Joining blocks on thread completion
• Thread is destroyed on join()

kwpm: threading and perl               12
Detaching threads



$thr->detach();

• Thread will not block on completion
• Thread will never become joinable
• However, you must ensure that the
  thread completes before your
  program terminates


kwpm: threading and perl                13
Synchronizing completion

my $t1 = threads->create(   sub { sleep 1; } );
my $t2 = threads->create(   sub { sleep 2; } );
$t2->detach();
my $t3 = threads->create(   sub { sleep 5; } );
my $t4 = threads->create(   sub { sleep 5; } );
$t4->detach();
sleep 3;

# yields:
Perl exited with active threads:
        1 running and unjoined
        1 finished and unjoined
        1 running and detached



kwpm: threading and perl                          14
Synchronizing completion (cont)

use threads;
local $SIG{INT} = sub { die };

my $j1 = threads->create(       sub   {   sleep   1; } );
my $d2 = threads->create(       sub   {   sleep   2; } );
my $j3 = threads->create(       sub   {   sleep   5; } );
my $d4 = threads->create(       sub   {   sleep   50; } );
$d2->detach();
$d4->detach();

$j1->join();               # joinable are joined
$j3->join();
$d4->kill('INT');          # detached are stopped
                           # d2 is ignored
sleep 1;                   # race!


kwpm: threading and perl                                     15
Other Basic Controls

• threads->yield()
     – Kernel hint: schedule something else.
       Might be a no-op, depending on
       implementation.
• threads->list()
     – Fetch list of all threads, or threads in
       certain states.
• my $tid = async {};
     – Just sugar for creating a thread with
       anonymous sub.

kwpm: threading and perl                          16
Shared Variables

• Nothing is shared by default

# compile-time:
my $foo :shared = 8;
my %hash :shared;

# runtime:
share( $bar );             # one level
shared_clone( $baz );      # deep share
kwpm: threading and perl                  17
Sharing Internals

my $foo :shared = 42;

SV = PVMG(0x1297048) at 0x1233c10
  REFCNT = 1
  FLAGS = (PADMY,GMG,SMG,pIOK)
  IV = 42
  NV = 0
  PV = 0
  MAGIC = 0x12549a0
    MG_VIRTUAL = 0x528e8040
    MG_TYPE = PERL_MAGIC_shared_scalar(n)
    MG_FLAGS = 0x30
    MG_PTR = 0x12d4910 ""
kwpm: threading and perl                    18
The Implementation


• An extra thread is created for shared
  memory
• Each thread that has access to a
  shared variable gets a handle
  variable
• Which is essentially tie()ed to the
  shared thread’s version.


kwpm: threading and perl              19
The Costs


• Shared memory is thus horribly
  expensive.
• Somewhat unavoidable
     – Perl variables are complex, and internal
       consistency needs to be arbitrated
     – Each shared variable has a mutex
       guarding it
     – No atomic types, strictly speaking, but
       this is close

kwpm: threading and perl                          20
Atomic assignment, but no atomic test

my $cnt :shared = 0;
my $t1 = threads->create( &work, $cnt );
my $t2 = threads->create( &work, $cnt );
$t1->join();
$t2->join();

sub work {
    my $cnt = shift;
    do {
         $$cnt++;
         print "$$cntn";
    } while $$cnt < 5;
}
# prints 1–6, add a sleep, and 1-5


kwpm: threading and perl                     21
Locking Example

# locks are very basic:

sub work {
    my $cnt = shift;
    while (1) {
        lock( $$cnt );          # lock held by scope
        print $$cnt++ . "n";   # meaning inc and
        last if $$cnt >= 5;     # cmp are now atomic
    }
}

# will always print 1..5




kwpm: threading and perl                               22
Locks


• Held only by scope
• Take a shared variable as argument
• Block until thread has exclusive lock

• There is no ‘try_lock’
• Deadlocks are easy



kwpm: threading and perl                  23
Deadlock

my $foo :shared = 0;
my $bar :shared = 0;

my $t1 = threads->create(
    sub { lock( $foo ); sleep 1; lock( $bar ); } );
my $t2 = threads->create(
    sub { lock( $bar ); sleep 1; lock( $foo ); } );

# threads block until killed




kwpm: threading and perl                              24
cond_wait and cond_broadcast


• Only one more real set of primitives
  in threaded Perl:
     – cond_wait( $var );
           • Block until another thread broadcasts for
             this shared $var
     – cond_broadcast( $var );
           • Notify all users waiting on this shared $var
• No guarantee the value changed,
  just as simple as that.

kwpm: threading and perl                                    25
Busy Wait versus Intelligent Wait


• Why?

# busy wait:
1 until ( $shr_flag == 1 );
# lazy wait:
sleep 1 until ( $flag == 1 );

# smart:
lock( $flag );
cond_wait( $flag ) until $flag == 1;
# but all modifiers need to broadcast
kwpm: threading and perl                26
Threadsafe Queue


• Can build complex structures from
  these primitives
• I’ll illustrate this and provide an
  example for cond_* at the same
  time

• Thread safe queue: want a FIFO pipe
  that can be used by arbitrary number
  of threads
kwpm: threading and perl                27
Queue implementation

my @queue :shared;

sub enqueue {
    lock( @queue );
    push @queue, @_;
    cond_broadcast( @queue );
}

sub dequeue {
    lock( @queue );
    cond_wait( @queue ) until @queue > 0;
    return shift @queue;
}
kwpm: threading and perl                    28
cond_signal()

my @queue :shared;

sub enqueue {
    lock( @queue );
    push @queue, @_;
    cond_signal( @queue );
}

sub dequeue {
    lock( @queue );
    cond_wait( @queue ) until @queue > 0;
    cond_signal( @queue ) if @queue > 1;
    return shift @queue;
}

kwpm: threading and perl                    29
Objectify!

package Queue;
use threads;
use threads::shared;

sub new {
    my $class = shift;
    my @queue :shared;
    return bless @queue, $class;
}

sub enqueue {
    my $self = shift;
    lock( @$self );
    push @$self, @_;
    cond_signal( @$self );
} # etc...
kwpm: threading and perl            30
Best Practices in Perl threads


• Best to abstract shared access into
  objects
• Avoid having multiple locks at the
  same time
     – More generally, never do something that
       might block while you have a lock.
• Minimize shared memory
• Avoid having APIs that will share
  user variables: copy instead
kwpm: threading and perl                     31
Beautiful Queues


• Queues turn out to be supremely
  useful in communication between
  threads:
     – One thread can pump a thread full of
       tasks, many can pull from queue and
       satisfy tasks
     – Workers can shove results into a queue
       for some reducer/reporter



kwpm: threading and perl                        32
Thread::Queue


• Thread::Queue, don’t need to build
  your own
• Also implements
     – non-blocking dequeue()
     – pending()
     – peek()




kwpm: threading and perl               33
Graceful degradation


• All of our Queue code (and
  Thread::Queue) works in non-
  threaded applications
• thread::shared (lock(), share(),
  :share, cond_*) degrades
  meaningfully in the absence of
    threads
• Can have threading-safe logic in
  modules
kwpm: threading and perl             34
Concurrent Programming


• Synchronization ‘Primitives’
     – Semaphores
     – Read/Write Locks
     – Thread Barriers
     – Critical Sections/Mutexes
     – Spinlocks
• None are provided as core primitives,
  but can be constructed from what we
  are given
kwpm: threading and perl              35
A Helpful Exercise


• Concurrent programming requires
  different mental model
• Execution environment is not
  necessarily consistent
• Race conditions are very hard to find
  in testing
• I found it a very helpful exercise to
  carefully implement generic
  synchronization mechanisms
kwpm: threading and perl              36
Semaphores


• The classic synchronization primitive
  [Dijkstra74]
• A type with an integer value, and
  two operations: up and down
     – up() increases the value
     – down() decreases the value, or blocks
       until the value can be decreased



kwpm: threading and perl                       37
Building a Semaphore

package Semaphore;
sub new {
    my ( $class, $initial ) = @_;
    my $val :shared = $initial;
    return bless $val, $class;
}
sub up {
    my $self = shift;
    lock( $$self );
    $$self++;
    cond_signal( $$self );
}
sub down {
    my $self = shift;
    lock( $$self );
    cond_wait( $$self ) while ( $$self <= 0 );
    $$self--;
}
kwpm: threading and perl                         38
Thread::Semaphore


• CPAN implementation:
  Thread::Semaphore
• Very useful for allocating from
  resource pools, particularly
  collaborative resources
• Dining Philosophers




kwpm: threading and perl            39
Read/Write Locks


• Resource guarded by a read write
  lock can:
     – Have many simultaneous readers, or
     – One writer.
• Readers block until no writer
• Writer blocks until no readers
• Useful for compound objects
     – Don’t want to read from something in an
       inconsistent state
kwpm: threading and perl                     40
Read/Write locks Build!

sub new {                               # last one, I promise
    my ( $class ) = @_;
    my %self :shared = (
        readers => 0,
        writers => 0,
        writers_waiting => 0,
    );
    return bless %self, $class;
}

sub read_lock {
    my $self = shift;
    lock( %$self );
    cond_wait( %$self )
        until ( $self->{writers_waiting} == 0
                and $self->{writers} == 0 );
    $self->{readers}++;
}
kwpm: threading and perl                                        41
Read/Write locks con’t

sub read_release {
    my $self = shift;
    lock( %$self );
    $self->{readers}--;
    cond_signal( %$self )
        if ( $self->{writers_waiting} > 0 );
}

sub write_lock {
    my $self = shift;
    lock( %$self );
    $self->{writers_waiting}++;
    cond_wait( %$self )
        until ( $self->{readers} == 0
                 and $self->{writers} == 0 );
    $self->{writers_waiting}--;
    $self->{writers}++;
}
kwpm: threading and perl                        42
Read/Write locks con’t

sub write_release {
    my $self = shift;
    lock( %$self );
    $self->{writers}--;
    cond_broadcast( %$self );
}




kwpm: threading and perl        43
Using Read/Write Locks

# in a worker thread
  $wrlock->read_lock();
# make a decision based on complex object
  $wrlock->read_release();



# in some maintenance thread
  $wrlock->write_lock();
# incrementally update object
  $wrlock->write_release();


• Common pattern where you have lots of readers,
  occasional updates

kwpm: threading and perl                       44
Critical Sections and Claims


• Critical section: only one thread
  can execute a section of code at a
  time.
• Can make claim objects for scoped
  claims
• Trivially implemented with
  Semaphores, won’t illustrate
  implementation

kwpm: threading and perl               45
Critical Section usage

my $cs = CriticalSection->new();

sub some_non_reentrant_sub
{
    my $claim = CriticalSection::Claim->new( $cs );

       # update some file
       # claim’s destructor release critical section
}


• Common pattern where you are altering a static
  file, or using some non-threadsafe code.


kwpm: threading and perl                               46
Thread::CriticalSection


• There is a beta module on CPAN,
  with a different approach:
my $cs = Thread::CriticalSection->new;
$cs->execute(
   sub {
       # your code is protected by $cs
   }
);


• Probably a much Perl-ier API

kwpm: threading and perl                 47
Thread Barriers


• A call that blocks until a certain
  threshold of waiters is met

my $barrier = Barrier->new( 5 );

# ...
# thread initialization

if ( $barrier->wait() ) {
    print "All set, starting!n";
}
# ...
kwpm: threading and perl               48
Thread::Barrier


• CPAN module with more or less that
  interface
• Unblocks all, but returns true for the
  thread that hit the threshold, so you
  can execute code once
• Barriers can be re-used, thresholds
  adjusted
• Likely most useful to block all
  threads of a class
kwpm: threading and perl                   49
Spinlocks


• Tight loop test and set:
1 until my_try_lock( $lock );

• Common in OS context, make little
  sense in user threads
• Mostly used for creating tiny critical
  sections in kernel structures in MP
  systems
     – Want to wait, but don’t want to release
       CPU
kwpm: threading and perl                         50
Why use Threads?

• Cons:
     – Not lightweight
     – Shared memory very expensive
     – Very hard to debug
     – Less portable
     – Suck the cost of context switching
     – Issues with XS modules
     – Core dumps and instability
     – Way slower than multiplexing
     – Threaded Perl is a compile-time option
kwpm: threading and perl                        51
Why use Threads? (cont)


• Pros:
     – Communication/sharing easier than
       between processes
     – Take advantage of multi-core systems
     – Easier to use an existing module in
       threads than to implement with non-
       blocking IO and multiplexing
           • Assuming module is thread safe
     – Sort of neat*

kwpm: threading and perl                      52
What’s on CPAN?


• Thread::Pool and friends
     – Cool idea – worker pools with monitored
       queues, snapshots, collectors, etc.
     – Poorly executed.
• Various synchronization methods
  discussed
• Some task-specific things, like
  threaded DNS resolvers
• Not much in the way of usable
  frameworks
kwpm: threading and perl                     53
List of Mentioned Modules


• Core Modules:
     – threads
     – threads::shared
     – Thread::Queue
     – Thread::Semaphore
• Non-core CPAN modules:
     – Thread::RWLock
     – Thread::CriticalSection
     – Thread::Barrier
kwpm: threading and perl         54
References and Useful Links


• Core threads module docs
     – http://perldoc.perl.org/threads.html
• Core threads::shared module docs
     – http://perldoc.perl.org/threads/shared.html
• perlthrtut threads tutorial
     – http://perldoc.perl.org/perlthrtut.html
     – This is an extensive and strongly
       recommended doc

kwpm: threading and perl                         55

Weitere ähnliche Inhalte

Was ist angesagt?

Fiber in the 10th year
Fiber in the 10th yearFiber in the 10th year
Fiber in the 10th yearKoichi Sasada
 
Automatic Reference Counting @ Pragma Night
Automatic Reference Counting @ Pragma NightAutomatic Reference Counting @ Pragma Night
Automatic Reference Counting @ Pragma NightGiuseppe Arici
 
JavaOne 2012 - JVM JIT for Dummies
JavaOne 2012 - JVM JIT for DummiesJavaOne 2012 - JVM JIT for Dummies
JavaOne 2012 - JVM JIT for DummiesCharles Nutter
 
Java & low latency applications
Java & low latency applicationsJava & low latency applications
Java & low latency applicationsRuslan Shevchenko
 
JRuby and Invokedynamic - Japan JUG 2015
JRuby and Invokedynamic - Japan JUG 2015JRuby and Invokedynamic - Japan JUG 2015
JRuby and Invokedynamic - Japan JUG 2015Charles Nutter
 
Let'swift "Concurrency in swift"
Let'swift "Concurrency in swift"Let'swift "Concurrency in swift"
Let'swift "Concurrency in swift"Hyuk Hur
 
Dissecting the rabbit: RabbitMQ Internal Architecture
Dissecting the rabbit: RabbitMQ Internal ArchitectureDissecting the rabbit: RabbitMQ Internal Architecture
Dissecting the rabbit: RabbitMQ Internal ArchitectureAlvaro Videla
 
Modern Objective-C @ Pragma Night
Modern Objective-C @ Pragma NightModern Objective-C @ Pragma Night
Modern Objective-C @ Pragma NightGiuseppe Arici
 
The Year of JRuby - RubyC 2018
The Year of JRuby - RubyC 2018The Year of JRuby - RubyC 2018
The Year of JRuby - RubyC 2018Charles Nutter
 
Lock? We don't need no stinkin' locks!
Lock? We don't need no stinkin' locks!Lock? We don't need no stinkin' locks!
Lock? We don't need no stinkin' locks!Michael Barker
 
Fast as C: How to Write Really Terrible Java
Fast as C: How to Write Really Terrible JavaFast as C: How to Write Really Terrible Java
Fast as C: How to Write Really Terrible JavaCharles Nutter
 
Concurrency in Python
Concurrency in PythonConcurrency in Python
Concurrency in PythonMosky Liu
 
Large-scaled Deploy Over 100 Servers in 3 Minutes
Large-scaled Deploy Over 100 Servers in 3 MinutesLarge-scaled Deploy Over 100 Servers in 3 Minutes
Large-scaled Deploy Over 100 Servers in 3 MinutesHiroshi SHIBATA
 
Ruby Concurrency and EventMachine
Ruby Concurrency and EventMachineRuby Concurrency and EventMachine
Ruby Concurrency and EventMachineChristopher Spring
 
Troubleshooting RabbitMQ and services that use it
Troubleshooting RabbitMQ and services that use itTroubleshooting RabbitMQ and services that use it
Troubleshooting RabbitMQ and services that use itMichael Klishin
 
NativeBoost
NativeBoostNativeBoost
NativeBoostESUG
 

Was ist angesagt? (19)

RubyGems 3 & 4
RubyGems 3 & 4RubyGems 3 & 4
RubyGems 3 & 4
 
Fiber in the 10th year
Fiber in the 10th yearFiber in the 10th year
Fiber in the 10th year
 
Automatic Reference Counting @ Pragma Night
Automatic Reference Counting @ Pragma NightAutomatic Reference Counting @ Pragma Night
Automatic Reference Counting @ Pragma Night
 
JavaOne 2012 - JVM JIT for Dummies
JavaOne 2012 - JVM JIT for DummiesJavaOne 2012 - JVM JIT for Dummies
JavaOne 2012 - JVM JIT for Dummies
 
Java & low latency applications
Java & low latency applicationsJava & low latency applications
Java & low latency applications
 
JRuby and Invokedynamic - Japan JUG 2015
JRuby and Invokedynamic - Japan JUG 2015JRuby and Invokedynamic - Japan JUG 2015
JRuby and Invokedynamic - Japan JUG 2015
 
Let'swift "Concurrency in swift"
Let'swift "Concurrency in swift"Let'swift "Concurrency in swift"
Let'swift "Concurrency in swift"
 
Dissecting the rabbit: RabbitMQ Internal Architecture
Dissecting the rabbit: RabbitMQ Internal ArchitectureDissecting the rabbit: RabbitMQ Internal Architecture
Dissecting the rabbit: RabbitMQ Internal Architecture
 
DSLs in JavaScript
DSLs in JavaScriptDSLs in JavaScript
DSLs in JavaScript
 
Modern Objective-C @ Pragma Night
Modern Objective-C @ Pragma NightModern Objective-C @ Pragma Night
Modern Objective-C @ Pragma Night
 
Pfm technical-inside
Pfm technical-insidePfm technical-inside
Pfm technical-inside
 
The Year of JRuby - RubyC 2018
The Year of JRuby - RubyC 2018The Year of JRuby - RubyC 2018
The Year of JRuby - RubyC 2018
 
Lock? We don't need no stinkin' locks!
Lock? We don't need no stinkin' locks!Lock? We don't need no stinkin' locks!
Lock? We don't need no stinkin' locks!
 
Fast as C: How to Write Really Terrible Java
Fast as C: How to Write Really Terrible JavaFast as C: How to Write Really Terrible Java
Fast as C: How to Write Really Terrible Java
 
Concurrency in Python
Concurrency in PythonConcurrency in Python
Concurrency in Python
 
Large-scaled Deploy Over 100 Servers in 3 Minutes
Large-scaled Deploy Over 100 Servers in 3 MinutesLarge-scaled Deploy Over 100 Servers in 3 Minutes
Large-scaled Deploy Over 100 Servers in 3 Minutes
 
Ruby Concurrency and EventMachine
Ruby Concurrency and EventMachineRuby Concurrency and EventMachine
Ruby Concurrency and EventMachine
 
Troubleshooting RabbitMQ and services that use it
Troubleshooting RabbitMQ and services that use itTroubleshooting RabbitMQ and services that use it
Troubleshooting RabbitMQ and services that use it
 
NativeBoost
NativeBoostNativeBoost
NativeBoost
 

Andere mochten auch

Best Methodology POV
Best Methodology POVBest Methodology POV
Best Methodology POVHani Gamal
 
Consort Brochure 3fold
Consort Brochure 3foldConsort Brochure 3fold
Consort Brochure 3foldConsort, Inc.
 
Prezentácia Program Hodiny Angličtiny
Prezentácia Program Hodiny AngličtinyPrezentácia Program Hodiny Angličtiny
Prezentácia Program Hodiny Angličtinyguestc6c70b
 
Pepe Valls Preb Resultados 1ª Fase
Pepe Valls Preb Resultados 1ª FasePepe Valls Preb Resultados 1ª Fase
Pepe Valls Preb Resultados 1ª FasePablo Monfort
 
Merry Christmas
Merry ChristmasMerry Christmas
Merry ChristmasNikos
 
Hack your db before the hackers do
Hack your db before the hackers doHack your db before the hackers do
Hack your db before the hackers dofangjiafu
 
Olympic Bloopers London 2012 (Nikos)
Olympic Bloopers London 2012 (Nikos)Olympic Bloopers London 2012 (Nikos)
Olympic Bloopers London 2012 (Nikos)Nikos
 
班級經營有一套
班級經營有一套班級經營有一套
班級經營有一套clinic
 
Rohit Talwar - Convention 2020 - Venue and Destination Innovation - Imex 25-...
Rohit Talwar - Convention 2020 - Venue and Destination Innovation  - Imex 25-...Rohit Talwar - Convention 2020 - Venue and Destination Innovation  - Imex 25-...
Rohit Talwar - Convention 2020 - Venue and Destination Innovation - Imex 25-...Rohit Talwar
 
Cute Kittens ( Nikos)
Cute Kittens ( Nikos)Cute Kittens ( Nikos)
Cute Kittens ( Nikos)Nikos
 
Greek kiosks
Greek kiosksGreek kiosks
Greek kiosksNikos
 
Mobile web application production for business
Mobile web application production for businessMobile web application production for business
Mobile web application production for businessHani Gamal
 
【エイズ孤児】
【エイズ孤児】【エイズ孤児】
【エイズ孤児】IDDP UK
 
英語童詩童謠
英語童詩童謠英語童詩童謠
英語童詩童謠clinic
 

Andere mochten auch (20)

Best Methodology POV
Best Methodology POVBest Methodology POV
Best Methodology POV
 
Consort Brochure 3fold
Consort Brochure 3foldConsort Brochure 3fold
Consort Brochure 3fold
 
Prezentácia Program Hodiny Angličtiny
Prezentácia Program Hodiny AngličtinyPrezentácia Program Hodiny Angličtiny
Prezentácia Program Hodiny Angličtiny
 
Pepe Valls Preb Resultados 1ª Fase
Pepe Valls Preb Resultados 1ª FasePepe Valls Preb Resultados 1ª Fase
Pepe Valls Preb Resultados 1ª Fase
 
Merry Christmas
Merry ChristmasMerry Christmas
Merry Christmas
 
Hack your db before the hackers do
Hack your db before the hackers doHack your db before the hackers do
Hack your db before the hackers do
 
Olympic Bloopers London 2012 (Nikos)
Olympic Bloopers London 2012 (Nikos)Olympic Bloopers London 2012 (Nikos)
Olympic Bloopers London 2012 (Nikos)
 
班級經營有一套
班級經營有一套班級經營有一套
班級經營有一套
 
Rohit Talwar - Convention 2020 - Venue and Destination Innovation - Imex 25-...
Rohit Talwar - Convention 2020 - Venue and Destination Innovation  - Imex 25-...Rohit Talwar - Convention 2020 - Venue and Destination Innovation  - Imex 25-...
Rohit Talwar - Convention 2020 - Venue and Destination Innovation - Imex 25-...
 
Superlatives
SuperlativesSuperlatives
Superlatives
 
Cute Kittens ( Nikos)
Cute Kittens ( Nikos)Cute Kittens ( Nikos)
Cute Kittens ( Nikos)
 
Greek kiosks
Greek kiosksGreek kiosks
Greek kiosks
 
Mobile web application production for business
Mobile web application production for businessMobile web application production for business
Mobile web application production for business
 
【エイズ孤児】
【エイズ孤児】【エイズ孤児】
【エイズ孤児】
 
Fp
FpFp
Fp
 
Paz
PazPaz
Paz
 
57 power of inn
57 power of inn57 power of inn
57 power of inn
 
Paz
PazPaz
Paz
 
Hi
HiHi
Hi
 
英語童詩童謠
英語童詩童謠英語童詩童謠
英語童詩童謠
 

Ähnlich wie 2008 07-24 kwpm-threads_and_synchronization

openmp.ppt
openmp.pptopenmp.ppt
openmp.pptFAfazi1
 
Perl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersPerl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersKirk Kimmel
 
Tuning parallelcodeonsolaris005
Tuning parallelcodeonsolaris005Tuning parallelcodeonsolaris005
Tuning parallelcodeonsolaris005dflexer
 
Parallel and Distributed Computing Chapter 5
Parallel and Distributed Computing Chapter 5Parallel and Distributed Computing Chapter 5
Parallel and Distributed Computing Chapter 5AbdullahMunir32
 
Programming using Open Mp
Programming using Open MpProgramming using Open Mp
Programming using Open MpAnshul Sharma
 
Presentation on Shared Memory Parallel Programming
Presentation on Shared Memory Parallel ProgrammingPresentation on Shared Memory Parallel Programming
Presentation on Shared Memory Parallel ProgrammingVengada Karthik Rangaraju
 
Medical Image Processing Strategies for multi-core CPUs
Medical Image Processing Strategies for multi-core CPUsMedical Image Processing Strategies for multi-core CPUs
Medical Image Processing Strategies for multi-core CPUsDaniel Blezek
 
Some Rough Fibrous Material
Some Rough Fibrous MaterialSome Rough Fibrous Material
Some Rough Fibrous MaterialMurray Steele
 
Intro To .Net Threads
Intro To .Net ThreadsIntro To .Net Threads
Intro To .Net Threadsrchakra
 
Programming with Threads in Java
Programming with Threads in JavaProgramming with Threads in Java
Programming with Threads in Javakoji lin
 
Let's Talk Locks!
Let's Talk Locks!Let's Talk Locks!
Let's Talk Locks!C4Media
 
Intro to OpenMP
Intro to OpenMPIntro to OpenMP
Intro to OpenMPjbp4444
 
Threads and multi threading
Threads and multi threadingThreads and multi threading
Threads and multi threadingAntonio Cesarano
 

Ähnlich wie 2008 07-24 kwpm-threads_and_synchronization (20)

openmp.ppt
openmp.pptopenmp.ppt
openmp.ppt
 
openmp.ppt
openmp.pptopenmp.ppt
openmp.ppt
 
Lecture8
Lecture8Lecture8
Lecture8
 
Perl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersPerl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one liners
 
Tuning parallelcodeonsolaris005
Tuning parallelcodeonsolaris005Tuning parallelcodeonsolaris005
Tuning parallelcodeonsolaris005
 
Threads
ThreadsThreads
Threads
 
Parallel and Distributed Computing Chapter 5
Parallel and Distributed Computing Chapter 5Parallel and Distributed Computing Chapter 5
Parallel and Distributed Computing Chapter 5
 
Programming using Open Mp
Programming using Open MpProgramming using Open Mp
Programming using Open Mp
 
Subroutines
SubroutinesSubroutines
Subroutines
 
Open mp intro_01
Open mp intro_01Open mp intro_01
Open mp intro_01
 
Presentation on Shared Memory Parallel Programming
Presentation on Shared Memory Parallel ProgrammingPresentation on Shared Memory Parallel Programming
Presentation on Shared Memory Parallel Programming
 
Lecture7
Lecture7Lecture7
Lecture7
 
Medical Image Processing Strategies for multi-core CPUs
Medical Image Processing Strategies for multi-core CPUsMedical Image Processing Strategies for multi-core CPUs
Medical Image Processing Strategies for multi-core CPUs
 
Some Rough Fibrous Material
Some Rough Fibrous MaterialSome Rough Fibrous Material
Some Rough Fibrous Material
 
Intro To .Net Threads
Intro To .Net ThreadsIntro To .Net Threads
Intro To .Net Threads
 
Programming with Threads in Java
Programming with Threads in JavaProgramming with Threads in Java
Programming with Threads in Java
 
Let's Talk Locks!
Let's Talk Locks!Let's Talk Locks!
Let's Talk Locks!
 
Chap7 slides
Chap7 slidesChap7 slides
Chap7 slides
 
Intro to OpenMP
Intro to OpenMPIntro to OpenMP
Intro to OpenMP
 
Threads and multi threading
Threads and multi threadingThreads and multi threading
Threads and multi threading
 

Mehr von fangjiafu

Wce internals rooted_con2011_ampliasecurity
Wce internals rooted_con2011_ampliasecurityWce internals rooted_con2011_ampliasecurity
Wce internals rooted_con2011_ampliasecurityfangjiafu
 
Oracle forensics 101
Oracle forensics 101Oracle forensics 101
Oracle forensics 101fangjiafu
 
Understanding and selecting_dsp_final
Understanding and selecting_dsp_finalUnderstanding and selecting_dsp_final
Understanding and selecting_dsp_finalfangjiafu
 
Wce12 uba ampliasecurity_eng
Wce12 uba ampliasecurity_engWce12 uba ampliasecurity_eng
Wce12 uba ampliasecurity_engfangjiafu
 
Ddos analizi
Ddos analiziDdos analizi
Ddos analizifangjiafu
 
Bypass dbms assert
Bypass dbms assertBypass dbms assert
Bypass dbms assertfangjiafu
 
Cursor injection
Cursor injectionCursor injection
Cursor injectionfangjiafu
 
Create user to_sysdba
Create user to_sysdbaCreate user to_sysdba
Create user to_sysdbafangjiafu
 
Presentation nix
Presentation nixPresentation nix
Presentation nixfangjiafu
 
Layer 7 ddos
Layer 7 ddosLayer 7 ddos
Layer 7 ddosfangjiafu
 
Tlsoptimizationprint 120224194603-phpapp02
Tlsoptimizationprint 120224194603-phpapp02Tlsoptimizationprint 120224194603-phpapp02
Tlsoptimizationprint 120224194603-phpapp02fangjiafu
 
Presentation nix
Presentation nixPresentation nix
Presentation nixfangjiafu
 
Proper passwordhashing
Proper passwordhashingProper passwordhashing
Proper passwordhashingfangjiafu
 
Burp suite injection中的应用by小冰
Burp suite injection中的应用by小冰Burp suite injection中的应用by小冰
Burp suite injection中的应用by小冰fangjiafu
 
Network security
Network securityNetwork security
Network securityfangjiafu
 

Mehr von fangjiafu (20)

Wce internals rooted_con2011_ampliasecurity
Wce internals rooted_con2011_ampliasecurityWce internals rooted_con2011_ampliasecurity
Wce internals rooted_con2011_ampliasecurity
 
Oracle forensics 101
Oracle forensics 101Oracle forensics 101
Oracle forensics 101
 
Understanding and selecting_dsp_final
Understanding and selecting_dsp_finalUnderstanding and selecting_dsp_final
Understanding and selecting_dsp_final
 
Wce12 uba ampliasecurity_eng
Wce12 uba ampliasecurity_engWce12 uba ampliasecurity_eng
Wce12 uba ampliasecurity_eng
 
Ddos analizi
Ddos analiziDdos analizi
Ddos analizi
 
Bypass dbms assert
Bypass dbms assertBypass dbms assert
Bypass dbms assert
 
Cursor injection
Cursor injectionCursor injection
Cursor injection
 
Create user to_sysdba
Create user to_sysdbaCreate user to_sysdba
Create user to_sysdba
 
Presentation nix
Presentation nixPresentation nix
Presentation nix
 
Layer 7 ddos
Layer 7 ddosLayer 7 ddos
Layer 7 ddos
 
Tlsoptimizationprint 120224194603-phpapp02
Tlsoptimizationprint 120224194603-phpapp02Tlsoptimizationprint 120224194603-phpapp02
Tlsoptimizationprint 120224194603-phpapp02
 
Crypto hlug
Crypto hlugCrypto hlug
Crypto hlug
 
Presentation nix
Presentation nixPresentation nix
Presentation nix
 
Rr 7944
Rr 7944Rr 7944
Rr 7944
 
Proper passwordhashing
Proper passwordhashingProper passwordhashing
Proper passwordhashing
 
Burp suite injection中的应用by小冰
Burp suite injection中的应用by小冰Burp suite injection中的应用by小冰
Burp suite injection中的应用by小冰
 
Oech03
Oech03Oech03
Oech03
 
Unit07
Unit07Unit07
Unit07
 
Unit05
Unit05Unit05
Unit05
 
Network security
Network securityNetwork security
Network security
 

Kürzlich hochgeladen

Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdfChristopherTHyatt
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 

Kürzlich hochgeladen (20)

Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 

2008 07-24 kwpm-threads_and_synchronization

  • 1. Threading and Perl Introduction To Perl Threads and Basics of Concurrent Programming eric.maki for kwpm 24/07/2008
  • 2. Talk Outline • Thread Basics • Threads and Shared Memory API – For Perl 5.8.3 and above • Synchronization Mechanisms – What’s built in? – Generic synchronization primitives • What they are and how they are used • What/how you can build from Perl’s built-ins (or use from CPAN) kwpm: threading and perl 2
  • 3. What is a Thread? • Most generally: an execution context. • Threads normally provide a mechanism for managing multiple execution contexts within the same OS process. • Threads can interact via shared memory, rather than via IPC. kwpm: threading and perl 3
  • 4. Threads and Languages • Language approaches to Threads: – Java: Supported in the JVM, thus it is a core language element. – pthreads (POSIX Threads): a cross language standard used widely with C/C++. This is an API, not an implementation. – Python: Core piece of language, implementation is platform dependent. kwpm: threading and perl 4
  • 5. Threads versus Fibres • The term ‘thread’ normally refers to execution contexts created by user processes, but scheduled by the kernel. • ‘Fibres’ are execution contexts managed entirely in user-space. – Advantage here is selection and optimization of scheduling and context scope. – Obviously, also downsides. kwpm: threading and perl 5
  • 6. Perl and Threads: Versions • Interpreter Threads introduced in Perl 5.6, and widely available and reasonably stable in Perl 5.8. • Perl 5.5 had a different threading model, which continued to be supported up to 5.10. – It never progressed beyond experimental. – I won’t discuss “old style threads” further. kwpm: threading and perl 6
  • 7. ithreads basic model • Unlike most other thread implementations, in ithreads global variables are non-shared by default. • A new thread actually gets a copy of everything. • This includes the interpreter instance. • Model is based around explicit sharing. kwpm: threading and perl 7
  • 8. ithreads basic model (con’t) • Very basic model • No notion of thread priority • Minimal built in synchronization mechanisms: – No mutexes – No semaphores • Everything based around shared locks. kwpm: threading and perl 8
  • 9. Implementation • Employs pthreads where they are available (everywhere but Win32, I believe) • pthreads on Linux are treated as ‘special’ processes with a shared memory space • Win32 uses windows threading model (some impedance mismatch) kwpm: threading and perl 9
  • 10. Jumping in • We’ll jump into the code examples in Perl • Concurrent programming requires a different mindset from strictly sequential programming kwpm: threading and perl 10
  • 11. Create a thread use threads; my $thr = threads->create( &entry_point, @args ); • A new thread is created, with a – new interpreter, stack, etc., – new copies of all unshared globals. • Starts by calling entry_point( @args ) • When the sub returns, the thread is complete • No guarantees of ‘first execution’ kwpm: threading and perl 11
  • 12. Thread Death my $res = $thr->join(); • Return value of a thread is the return value of the function • When a thread completes it waits for the result to be read • Joining blocks on thread completion • Thread is destroyed on join() kwpm: threading and perl 12
  • 13. Detaching threads $thr->detach(); • Thread will not block on completion • Thread will never become joinable • However, you must ensure that the thread completes before your program terminates kwpm: threading and perl 13
  • 14. Synchronizing completion my $t1 = threads->create( sub { sleep 1; } ); my $t2 = threads->create( sub { sleep 2; } ); $t2->detach(); my $t3 = threads->create( sub { sleep 5; } ); my $t4 = threads->create( sub { sleep 5; } ); $t4->detach(); sleep 3; # yields: Perl exited with active threads: 1 running and unjoined 1 finished and unjoined 1 running and detached kwpm: threading and perl 14
  • 15. Synchronizing completion (cont) use threads; local $SIG{INT} = sub { die }; my $j1 = threads->create( sub { sleep 1; } ); my $d2 = threads->create( sub { sleep 2; } ); my $j3 = threads->create( sub { sleep 5; } ); my $d4 = threads->create( sub { sleep 50; } ); $d2->detach(); $d4->detach(); $j1->join(); # joinable are joined $j3->join(); $d4->kill('INT'); # detached are stopped # d2 is ignored sleep 1; # race! kwpm: threading and perl 15
  • 16. Other Basic Controls • threads->yield() – Kernel hint: schedule something else. Might be a no-op, depending on implementation. • threads->list() – Fetch list of all threads, or threads in certain states. • my $tid = async {}; – Just sugar for creating a thread with anonymous sub. kwpm: threading and perl 16
  • 17. Shared Variables • Nothing is shared by default # compile-time: my $foo :shared = 8; my %hash :shared; # runtime: share( $bar ); # one level shared_clone( $baz ); # deep share kwpm: threading and perl 17
  • 18. Sharing Internals my $foo :shared = 42; SV = PVMG(0x1297048) at 0x1233c10 REFCNT = 1 FLAGS = (PADMY,GMG,SMG,pIOK) IV = 42 NV = 0 PV = 0 MAGIC = 0x12549a0 MG_VIRTUAL = 0x528e8040 MG_TYPE = PERL_MAGIC_shared_scalar(n) MG_FLAGS = 0x30 MG_PTR = 0x12d4910 "" kwpm: threading and perl 18
  • 19. The Implementation • An extra thread is created for shared memory • Each thread that has access to a shared variable gets a handle variable • Which is essentially tie()ed to the shared thread’s version. kwpm: threading and perl 19
  • 20. The Costs • Shared memory is thus horribly expensive. • Somewhat unavoidable – Perl variables are complex, and internal consistency needs to be arbitrated – Each shared variable has a mutex guarding it – No atomic types, strictly speaking, but this is close kwpm: threading and perl 20
  • 21. Atomic assignment, but no atomic test my $cnt :shared = 0; my $t1 = threads->create( &work, $cnt ); my $t2 = threads->create( &work, $cnt ); $t1->join(); $t2->join(); sub work { my $cnt = shift; do { $$cnt++; print "$$cntn"; } while $$cnt < 5; } # prints 1–6, add a sleep, and 1-5 kwpm: threading and perl 21
  • 22. Locking Example # locks are very basic: sub work { my $cnt = shift; while (1) { lock( $$cnt ); # lock held by scope print $$cnt++ . "n"; # meaning inc and last if $$cnt >= 5; # cmp are now atomic } } # will always print 1..5 kwpm: threading and perl 22
  • 23. Locks • Held only by scope • Take a shared variable as argument • Block until thread has exclusive lock • There is no ‘try_lock’ • Deadlocks are easy kwpm: threading and perl 23
  • 24. Deadlock my $foo :shared = 0; my $bar :shared = 0; my $t1 = threads->create( sub { lock( $foo ); sleep 1; lock( $bar ); } ); my $t2 = threads->create( sub { lock( $bar ); sleep 1; lock( $foo ); } ); # threads block until killed kwpm: threading and perl 24
  • 25. cond_wait and cond_broadcast • Only one more real set of primitives in threaded Perl: – cond_wait( $var ); • Block until another thread broadcasts for this shared $var – cond_broadcast( $var ); • Notify all users waiting on this shared $var • No guarantee the value changed, just as simple as that. kwpm: threading and perl 25
  • 26. Busy Wait versus Intelligent Wait • Why? # busy wait: 1 until ( $shr_flag == 1 ); # lazy wait: sleep 1 until ( $flag == 1 ); # smart: lock( $flag ); cond_wait( $flag ) until $flag == 1; # but all modifiers need to broadcast kwpm: threading and perl 26
  • 27. Threadsafe Queue • Can build complex structures from these primitives • I’ll illustrate this and provide an example for cond_* at the same time • Thread safe queue: want a FIFO pipe that can be used by arbitrary number of threads kwpm: threading and perl 27
  • 28. Queue implementation my @queue :shared; sub enqueue { lock( @queue ); push @queue, @_; cond_broadcast( @queue ); } sub dequeue { lock( @queue ); cond_wait( @queue ) until @queue > 0; return shift @queue; } kwpm: threading and perl 28
  • 29. cond_signal() my @queue :shared; sub enqueue { lock( @queue ); push @queue, @_; cond_signal( @queue ); } sub dequeue { lock( @queue ); cond_wait( @queue ) until @queue > 0; cond_signal( @queue ) if @queue > 1; return shift @queue; } kwpm: threading and perl 29
  • 30. Objectify! package Queue; use threads; use threads::shared; sub new { my $class = shift; my @queue :shared; return bless @queue, $class; } sub enqueue { my $self = shift; lock( @$self ); push @$self, @_; cond_signal( @$self ); } # etc... kwpm: threading and perl 30
  • 31. Best Practices in Perl threads • Best to abstract shared access into objects • Avoid having multiple locks at the same time – More generally, never do something that might block while you have a lock. • Minimize shared memory • Avoid having APIs that will share user variables: copy instead kwpm: threading and perl 31
  • 32. Beautiful Queues • Queues turn out to be supremely useful in communication between threads: – One thread can pump a thread full of tasks, many can pull from queue and satisfy tasks – Workers can shove results into a queue for some reducer/reporter kwpm: threading and perl 32
  • 33. Thread::Queue • Thread::Queue, don’t need to build your own • Also implements – non-blocking dequeue() – pending() – peek() kwpm: threading and perl 33
  • 34. Graceful degradation • All of our Queue code (and Thread::Queue) works in non- threaded applications • thread::shared (lock(), share(), :share, cond_*) degrades meaningfully in the absence of threads • Can have threading-safe logic in modules kwpm: threading and perl 34
  • 35. Concurrent Programming • Synchronization ‘Primitives’ – Semaphores – Read/Write Locks – Thread Barriers – Critical Sections/Mutexes – Spinlocks • None are provided as core primitives, but can be constructed from what we are given kwpm: threading and perl 35
  • 36. A Helpful Exercise • Concurrent programming requires different mental model • Execution environment is not necessarily consistent • Race conditions are very hard to find in testing • I found it a very helpful exercise to carefully implement generic synchronization mechanisms kwpm: threading and perl 36
  • 37. Semaphores • The classic synchronization primitive [Dijkstra74] • A type with an integer value, and two operations: up and down – up() increases the value – down() decreases the value, or blocks until the value can be decreased kwpm: threading and perl 37
  • 38. Building a Semaphore package Semaphore; sub new { my ( $class, $initial ) = @_; my $val :shared = $initial; return bless $val, $class; } sub up { my $self = shift; lock( $$self ); $$self++; cond_signal( $$self ); } sub down { my $self = shift; lock( $$self ); cond_wait( $$self ) while ( $$self <= 0 ); $$self--; } kwpm: threading and perl 38
  • 39. Thread::Semaphore • CPAN implementation: Thread::Semaphore • Very useful for allocating from resource pools, particularly collaborative resources • Dining Philosophers kwpm: threading and perl 39
  • 40. Read/Write Locks • Resource guarded by a read write lock can: – Have many simultaneous readers, or – One writer. • Readers block until no writer • Writer blocks until no readers • Useful for compound objects – Don’t want to read from something in an inconsistent state kwpm: threading and perl 40
  • 41. Read/Write locks Build! sub new { # last one, I promise my ( $class ) = @_; my %self :shared = ( readers => 0, writers => 0, writers_waiting => 0, ); return bless %self, $class; } sub read_lock { my $self = shift; lock( %$self ); cond_wait( %$self ) until ( $self->{writers_waiting} == 0 and $self->{writers} == 0 ); $self->{readers}++; } kwpm: threading and perl 41
  • 42. Read/Write locks con’t sub read_release { my $self = shift; lock( %$self ); $self->{readers}--; cond_signal( %$self ) if ( $self->{writers_waiting} > 0 ); } sub write_lock { my $self = shift; lock( %$self ); $self->{writers_waiting}++; cond_wait( %$self ) until ( $self->{readers} == 0 and $self->{writers} == 0 ); $self->{writers_waiting}--; $self->{writers}++; } kwpm: threading and perl 42
  • 43. Read/Write locks con’t sub write_release { my $self = shift; lock( %$self ); $self->{writers}--; cond_broadcast( %$self ); } kwpm: threading and perl 43
  • 44. Using Read/Write Locks # in a worker thread $wrlock->read_lock(); # make a decision based on complex object $wrlock->read_release(); # in some maintenance thread $wrlock->write_lock(); # incrementally update object $wrlock->write_release(); • Common pattern where you have lots of readers, occasional updates kwpm: threading and perl 44
  • 45. Critical Sections and Claims • Critical section: only one thread can execute a section of code at a time. • Can make claim objects for scoped claims • Trivially implemented with Semaphores, won’t illustrate implementation kwpm: threading and perl 45
  • 46. Critical Section usage my $cs = CriticalSection->new(); sub some_non_reentrant_sub { my $claim = CriticalSection::Claim->new( $cs ); # update some file # claim’s destructor release critical section } • Common pattern where you are altering a static file, or using some non-threadsafe code. kwpm: threading and perl 46
  • 47. Thread::CriticalSection • There is a beta module on CPAN, with a different approach: my $cs = Thread::CriticalSection->new; $cs->execute( sub { # your code is protected by $cs } ); • Probably a much Perl-ier API kwpm: threading and perl 47
  • 48. Thread Barriers • A call that blocks until a certain threshold of waiters is met my $barrier = Barrier->new( 5 ); # ... # thread initialization if ( $barrier->wait() ) { print "All set, starting!n"; } # ... kwpm: threading and perl 48
  • 49. Thread::Barrier • CPAN module with more or less that interface • Unblocks all, but returns true for the thread that hit the threshold, so you can execute code once • Barriers can be re-used, thresholds adjusted • Likely most useful to block all threads of a class kwpm: threading and perl 49
  • 50. Spinlocks • Tight loop test and set: 1 until my_try_lock( $lock ); • Common in OS context, make little sense in user threads • Mostly used for creating tiny critical sections in kernel structures in MP systems – Want to wait, but don’t want to release CPU kwpm: threading and perl 50
  • 51. Why use Threads? • Cons: – Not lightweight – Shared memory very expensive – Very hard to debug – Less portable – Suck the cost of context switching – Issues with XS modules – Core dumps and instability – Way slower than multiplexing – Threaded Perl is a compile-time option kwpm: threading and perl 51
  • 52. Why use Threads? (cont) • Pros: – Communication/sharing easier than between processes – Take advantage of multi-core systems – Easier to use an existing module in threads than to implement with non- blocking IO and multiplexing • Assuming module is thread safe – Sort of neat* kwpm: threading and perl 52
  • 53. What’s on CPAN? • Thread::Pool and friends – Cool idea – worker pools with monitored queues, snapshots, collectors, etc. – Poorly executed. • Various synchronization methods discussed • Some task-specific things, like threaded DNS resolvers • Not much in the way of usable frameworks kwpm: threading and perl 53
  • 54. List of Mentioned Modules • Core Modules: – threads – threads::shared – Thread::Queue – Thread::Semaphore • Non-core CPAN modules: – Thread::RWLock – Thread::CriticalSection – Thread::Barrier kwpm: threading and perl 54
  • 55. References and Useful Links • Core threads module docs – http://perldoc.perl.org/threads.html • Core threads::shared module docs – http://perldoc.perl.org/threads/shared.html • perlthrtut threads tutorial – http://perldoc.perl.org/perlthrtut.html – This is an extensive and strongly recommended doc kwpm: threading and perl 55