Home > AS/400 Tips > iSeries administrator tips > Introduction to multi-threaded programming -- part 2
iSeries 400 Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ISERIES ADMINISTRATOR TIPS

Introduction to multi-threaded programming -- part 2


Ron Turull
11.21.2006
Rating: -4.27- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



Ron Turull

Multi-threaded programming was introduced on the AS/400-iSeries back in V4R2, but was limited to C and Java programs. Multithreaded programming took a big step up with the release of V4R4 when multithreaded support was added to the RPG and COBOL languages. In an earlier tip, Introduction to multi-threaded programming -- part 1, we discussed the basics of multithreaded programming and how it is advantageous to both you and your users. Now, let's take a look at how you actually go about creating a multi-threading program.

The mechanics of multithreaded programming

When a job starts on the System i/iSeries, the system creates a primary thread for that job. All programs and procedures are run in that primary thread unless you explicitly spawn off additional threads.

In procedure-based languages such as C and RPG, you use the appropriate API to spawn off secondary threads (e.g., the pthread_create API). One of the parameters that you send the API is a pointer to a procedure that will get control once the secondary thread has been established. This is called the start routine and must conform to a standard interface. This procedure will then dictate the course that the thread will take.

Code running in the secondary thread executes independently of all other threads although some data areas, such as global data, are shared. You end a thread by either calling an appropriate API (e.g., pthread_exit) or simply returning from its start routine; however, it does not automatically "return" to the thread that created it, which is off running its own code -- like a called procedure does.

Consider this example. Thread A spawns (i.e., creates) thread B. Thread A and thread B are now executing simultaneously. When thread B ends it is left in a sort of "limbo" state until its resources are freed up by thread A calling the pthread_detachAPI. Before calling the pthread_detach API however, thread A can call the pthread_join API to retrieve thread B's return value (called the status). If thread B is still running, the pthread_join API will wait for thread B to end and then retrieve its return value.

The lingo of multi-threaded programming

Here is a quick glossary of the basic terms of multithreaded programming.

  • Threads API set. A set of APIs that allow you to do multithreaded programming. There are several sets on the AS/400-iSeries. For example, there is the Pthread API set which conforms to the POSIX standard. In Java, you use the threads predefined classes.

  • Spawn. To create a secondary thread.

  • Start routine. The procedure or function that gets control when a new thread is spawned.

  • Join. To retrieve the return value from a secondary thread that has ended but hasn't been detached yet. If the thread has not yet ended, the joining thread will be placed in a "join wait" state until the thread has ended. A number of threads can "join with" (i.e., retrieve the return value of) a secondary thread until it is detached.

  • Detach. To delete a thread that has ended. Most thread resources are released when a thread is ended. Any remaining locked resources are released when the thread is detached.

  • Threadsafe. Code that is designed for and will not cause unexpected results in a multithreaded environment.

    -----------------------------------
    About the author: Ron Turull is editor of Inside Version 5. He has more than 20 years' experience programming for and managing AS/400-iSeries systems.


    Rate this Tip
    To rate tips, you must be a member of Search400.com.
    Register now to start rating these tips. Log in if you are already a member.


    Submit a Tip




    Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



    RELATED CONTENT
    iSeries administrator tips
    Analyze the health of your IBM i server with iScore
    Researching high availability for your System i shop
    Translating Linux for IBM i admins: Using GUI to make it easy
    Translating Linux for IBM i admins: Working with jobs and networking
    OpenOffice: What to know before making the transition from Microsoft Office
    OpenOffice: An enterprise open source solution
    Database performance comparisons on IBM i
    Translating Linux for IBM i admins: User profile commands
    Modern System i reports using Client Access
    Tips for installing Lotus Domino server on a System i partition

    RELATED RESOURCES
    2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
    Search Bitpipe.com for the latest white papers and business webcasts
    Whatis.com, the online computer dictionary

    DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



  • iSeries Security - Security Tools, Physical Security and System Security
    HomeNewsTopicsITKnowledge ExchangeTipsBlogsAsk the ExpertsMultimediaWhite PapersProducts
    About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
    SEARCH 
    TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

    TechTarget Corporate Web Site  |  Media Kits  |  Site Map




    All Rights Reserved, Copyright 1999 - 2009, TechTarget | Read our Privacy Policy
      TechTarget - The IT Media ROI Experts