+ All Categories
Home > Documents > Pthreads Programming

Pthreads Programming

Date post: 03-Jun-2018
Category:
Upload: gigiberlogea
View: 282 times
Download: 0 times
Share this document with a friend

of 54

Transcript
  • 8/12/2019 Pthreads Programming

    1/54

    Pthreads Programming: A

    Hands-on Introduction

    Adrien Lamothe

    [email protected]

    Monday, July 23, 2007

    8:30 am !2:00 noon

    "oom #!3$%!&0Copyright 2007 Adrien LamotheAll Rights Resered

    mailto:[email protected]:[email protected]
  • 8/12/2019 Pthreads Programming

    2/54

    Agenda

    We're going to cover fundamental concepts of multi-threaded

    programming and Pthreads. We'll play around with some simple C

    programs, that utilize Pthreads, to learn the basics and to illustrate

    some important concepts. Due to time limitation, we may not be able

    to tal about some of the finer points of Pthreads programming.

    !his is not a comprehensive tutorial. !he goal is to get you "uicly

    familiar with multi-threaded programming concepts and Pthreadsprogramming. #f you haven't wored with threads before, this tutorial

    should serve as a good starting point. #f you are more e$perienced, you

    may or may not benefit from this tutorial% there will be some discussion

    that may interest those of you with nowledge and e$perience of the

    topic.

    !his set of slides will serve as our guide. &nce we get to the e$ample

    programs the activity will also involve actually running and changing

    the programs, in order to have some fun and see first-hand how

    Pthreads actually wor.

  • 8/12/2019 Pthreads Programming

    3/54

    Background

    Concurrent systems and multi-threadedprogramming have been with us a number of

    years.

    ecent developments (clusters, multi-coremicroprocessors) have led to renewed interest in

    concurrent programming.

  • 8/12/2019 Pthreads Programming

    4/54

    New Multicore Processors

    *un +icrosystems iagara / cores#0+ Cell 0roadband 1ngine (C01) up to 2 coresParalla$ Propeller Chip 3 cores#ntel Core / Duo, 4uad Core / and 5 cores

    6+D Dual Core (soon to release "uad core)

  • 8/12/2019 Pthreads Programming

    5/54

    Background

    #n the past, support for concurrent programmingwas uneven among vendors. Concurrent

    programming environments and tools were mostly

    confined to specialized applications.

    eal-time operating systems, in particular,

    strongly supported concurrent programming, due

    in some cases to a lac of memory protection.

    +any real-time systems also demand pearesponsiveness.

  • 8/12/2019 Pthreads Programming

    6/54

    Why Bother with Concurrent Programming?

    !he current trend in microprocessor design isparallelism.

    Cloc speed wasn't the answer, and 7hit the

    wall8.

    +ulti-core CP9s increase both performance and

    energy efficiency.

    Destop and noteboo computers are now

    parallel machines.

  • 8/12/2019 Pthreads Programming

    7/54

    Why Bother with Concurrent Programming?

    #f your software application demands high

    performance and:or responsiveness, you should

    consider enabling it to tae advantage of the

    parallelism inherent in today's computer systems.

  • 8/12/2019 Pthreads Programming

    8/54

    Why Bother with Concurrent Programming?

    ot all software benefits from concurrency.

    Profile your application.

    Consider the cost:benefits.

    !he learning curve is not as bad as you may

    thin.

  • 8/12/2019 Pthreads Programming

    9/54

    What types of software benefit?

    !he following conditions mar an application as a candidate for

    concurrency;

  • 8/12/2019 Pthreads Programming

    10/54

    Concurrent Software Architecture

    !wo main approaches to concurrent programming;

    Process +odel

    !hreading +odel

  • 8/12/2019 Pthreads Programming

    11/54

    Process Model

    6pplication distributed across multiple processes.

    Processes wor in parallel, running on separate

    e$ecution units (CP9s or cores.)

    9ni$ a great platform for Process +odel

    applications, due to a good set of interprocess

    functionality (shared memory, semaphores,process control)

  • 8/12/2019 Pthreads Programming

    12/54

    hreading Model

    Parallelism achieved within a single process.

    6dvantages;

  • 8/12/2019 Pthreads Programming

    13/54

    !ser Space "s# $ernel Space hreads

    9ser *pace !hreads (79ser !hreads8);

    Controlled by a scheduler lined into the process.Coordination with ernel scheduler re"uired.7Dynamic8 languages (Perl, Python, uby) use 9ser

    !hreads (each with their own semantics.) ?ava utilizes9ser !hreads (called 7green threads8.)

    @ernel *pace !hreads (7@ernel !hreads8);

    Controlled only by ernel scheduler.When supported well by the &*, the better way to

    implement threading.

  • 8/12/2019 Pthreads Programming

    14/54

    hreads in %inu&

    &riginal eneration P&*#A !hreads (>P!) was

    offered as a replacement for

  • 8/12/2019 Pthreads Programming

    15/54

    'endor Support

    !hreading support varied among vendors.

    #n 22E, a P&*#A standard, PBB.c (nown as

    7Pthreads8) was issued, to standardize multi-threaded

    programming. !his standard has largely remained the same,

    with some minor changes.

    Fendor support has improved (but # still don't trust themG)

  • 8/12/2019 Pthreads Programming

    16/54

    Current rends in Multi(hreaded Programming

    &pen+P (www.openmp.org) is a toolit that aims

    to simplify multi-threaded programming in C:CJJand K&!6.

    *ome compilers now have switches that cause

    generation of parallel code, without re"uiring any

    special programming by the developer. *un

    +icrosystems has this in their C:CJJ compiler.

    *un's motto is 7let the compiler handle thecomplications of producing multi-threaded code.8

    http://www.openmp.org/http://www.openmp.org/
  • 8/12/2019 Pthreads Programming

    17/54

    Current rends in Multi(hreaded Programming

    asell (www.hasell.org) is a language that aims to simplify multi-

    threaded programming. !his afternoon, in DL from ;B to E;BB pm,a asell tutorial will be presented.

    !ransactional +emory is a techni"ue to Iournal memory #:&, which

    promises to alleviate some of the potential pitfalls of multi-threaded

    programming. !here will be a short tal about this on Wednesday,

    2;Bam-2;5Epm, Portland 0allroom.

    !he >9 organization has developed a library for C, called >9 Pth

    (for >9 Portable !hreads, see www.gnu.org:software:pth). Pth was a

    foundation of the abandoned >P! proIect.

    >9 also has their own &pen+P proIect (called >&+P,) which appears

    to be inactive at this time.

    !here are other &pen+P implementations.

    http://www.haskell.org/http://www.gnu.org/software/pthhttp://www.gnu.org/software/pthhttp://www.haskell.org/
  • 8/12/2019 Pthreads Programming

    18/54

    Current rends in Multi(hreaded Programming

    #ntel Corporation has created a toolit, a CJJ class library, to

    simplify mutli-threaded programming and also has a debugger andprofiler for threaded software. #ntel plans to release a new open-source

    product for multi-threaded development at this &*C& (!uesday, 3;B

    am /;BB noon, D2-5B.)

    6 new &'eilly boo has Iust been released, titled #ntel !hreading0uilding 0locs.

  • 8/12/2019 Pthreads Programming

    19/54

    )ntel hreading Building Blocks *BB+

    #ntel has tied together a number of different parallel

    programming concepts and implemented them in !00.

    !00 is only available for #ntel processors and those that

    emulate them (i.e. 6+D). #ntel may open-source !00, in

    which case you will be able to rebuild it for differentplatforms.

    #f #ntel open-sources !00, be sure to chec whether critical

    performance functionality has been moved into the #ntelcompilers. 6lso carefully chec licensing.

    Do your own testing and benchmaring of !00.

  • 8/12/2019 Pthreads Programming

    20/54

    )ntel hreading Building Blocks *BB+

    !00 contains different types of mute$es, for different

    situations.

    !00 seems to do a good Iob supporting manipulation of

    comple$ data structures.

    !00 seems to be aimed at parallel mathematical

    operations.

    Comple$ity doesn't go away, it Iust gets e$pressed

    differently. #n terms of coding, !00 is in some ways more

    comple$ than woring with Pthreads.

    !00 is not standards compliant. #t is a proprietary 6P#.

  • 8/12/2019 Pthreads Programming

    21/54

    Pthreads

    Wh Pth d ?

  • 8/12/2019 Pthreads Programming

    22/54

    Why Pthreads?

    Pthreads is a good way to start learning about

    concurrent programming.

    Pthreads is an international standard. =1*, !6!

    #* 6 >&&D !#>G

  • 8/12/2019 Pthreads Programming

    23/54

    Why Pthreads?

    Pthreads get the Iob done.

    1$amples of successful Pthreads applications;

    6pache

    +y*4et over it.

    6ll computer systems share resources, in different

    areas. =es, you should minimize shared data

    access, but at some point your threads will need toshare some data.

    While on the subIect of shared data...

    )BM Cell Broadband 4ngine *CB45 or the 6Cell7+

  • 8/12/2019 Pthreads Programming

    45/54

    !he C01 has a main e$ecution core and up to 3 7*ynergistic

    Processing 1lements8 (*P1s). *P1s are e$ecution cores.What maes them different and special, is they each have

    their own memory. !his alleviates much of the trouble

    associated with shared memory.

    C01 has very fast data buses, to facilitate message passing

    and broadband data transmission.

    # find C01 the most interesting new processor architecture.

    )BM Cell Broadband 4ngine *CB45 or the 6Cell7+

  • 8/12/2019 Pthreads Programming

    46/54

    )BM Cell Broadband 4ngine *CB45 or the 6Cell7+

  • 8/12/2019 Pthreads Programming

    47/54

    #0+ has taen a hybrid approach to programming

    C01, adding a library with functions applying to the*P1's.

    1ach *P1 has dedicated memory. !his greatly

    reduces the need for threads to share physical

    memory.

    System Considerations for Concurrency8Parallelism

  • 8/12/2019 Pthreads Programming

    48/54

    !o achieve the best results from concurrency, the entire

    computer system needs to be designed with as much

    parallelism as possible. !his means things such as;

    Parallel networ interface controllers*imultaneous memory accessDedicated memory for e$ecution cores

    1ven so, there are always going to be bottlenecs, there is

    no way to escape this. !he way to minimize the effect of

    bottlenecs is to increase processing speed at bottlenecpoints. !hus, system design becomes an e$ercise in

    balancing bandwidth and concurrency.

    Problems to a"oid when programming threads

  • 8/12/2019 Pthreads Programming

    49/54

    ace Conditions

    Priority #nversion

    Deadly 1mbrace

    !hread unsafe libraries (due to reentrancy and

    thread cancellation)

    unning out of process resources

    hings to do when working with Pthreads

  • 8/12/2019 Pthreads Programming

    50/54

    @eep things as clean and simple as possible

    +inimize shared data

    9ther opics *time permitting+

  • 8/12/2019 Pthreads Programming

    51/54

    !hread *cheduling

    !hread Cancellation!est harnesses for our programsDebugging with >9 gdbCondition Fariables

    Process resources and thread attributesCleanup functions!hread cancellation*cheduling Pthreads

    *etting process resources

    Summary

    h i l h ti t i t d th t i d

  • 8/12/2019 Pthreads Programming

    52/54

    hours is only enough time to introduce the topic and

    loo at some simple Pthreads e$amples.

    Pthreads programming is "uite different than se"uential

    programming. #t involves thining a bit differently about

    what is happening with your code and is also a bit tricy.

    +any people are comparing concurrent programming to

    obIect-oriented programming, in that it re"uires a change ofthining to accommodate the paradigm shift. # thin this is a

    good analogy.

    Despite uneven implementation among vendors, Pthreads isproven technology that powers some of the most pervasive

    and successful infrastructure software.

    Summary *cont#+

    &f Pth d i 't f #f I ( t f

  • 8/12/2019 Pthreads Programming

    53/54

    &f course, Pthreads isn't for everyone. #f you enIoy (out of

    either pleasure or necessity) the advantages of woring with

    dynamic languages, then by all means continue woring withthem. =our programs can still benefit from the threading

    mechanisms of those languages. *ome people have

    developed Python e$tensions, written in C, that allow them

    to access Pthreads from their Python programs.

    !hose of you developing server software, demanding high

    performance, should definitely tae a close loo at Pthreads.

    Download +y*4< source code and see how they have

    harnessed Pthreads.

    #f your software is mathematically intensive, you should tae

    a loo at #ntel !hreading 0uilding 0locs (!00).

  • 8/12/2019 Pthreads Programming

    54/54

    !han" you #or coming$ en%oy the rest o# the sho&'

    Adrien Lamothe

    adrien(adriens&e)'com


Recommended