Announcement

Collapse
No announcement yet.

Threads & Thread-Safe

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Threads & Thread-Safe

    Hi,

    I hear these terms used quite often, however, I have never fully
    understood the use of threads until reading an article about
    how they can be used. (If I could remember where the article was
    I would have posted the link)

    For example; it talked about safe-threaded dll's, how many of the
    dll's "Used in Searching Type DLL's" use safe-threads to read files
    and search them simultaneously, which then returns the results of
    the search that much quicker!

    Although I did find some info on this site (we'll over my head,
    and don't mind admitting it) Terminology!

    Where's the best to find info concerning threads, use etc...?
    And what books do you know that will speak in layman terms, for
    those of us who are not familiar with the subject mater? Lastly,
    does anyone have any (simple thread examples), that won't be
    too difficult to pick upon and learn from.

    Thanks

    MWM




    ------------------
    mwm
    mwm

  • #2
    Thread programming can be VERY useful but it is not necessarily the be-all-and-end-all solution for all situations.

    Whilst threads execute "concurrently", each CPU can only be processing one thread at a time, forming a thread "queue".

    Obviously, if you have more than one CPU, then you can get true (well, better) concurrency for the same number of threads (that have the same thread priority).

    Looking at the single CPU situation, the main point to remember is that context switching (like task switching) takes a finite amount of time to occur, so if you have two threads searching the same disk file on a single CPU system, it could end being slower than using just one thread, purely because there are no context-switches occurring (and therefore less overhead).

    Also, thread-safe programming adds a whole new dimension to programming. Even simple code can be unsafe, and subs/functions that are called by a thread has to be thread-safe too.

    In essence, thread code that uses GLOBAL or STATIC variables (and are not stored as a single 32-bit wide variable) *requires* you to use synchronization to avoid the possibility of one thread partially updating the data when a contect switch allows another thread to read the partially updated data.

    For example, lets look at the situation where Thread 1 stored "Hello World!" in a global asciiz string, and soon after, thread 2 was going to write "Goodbye!" into the same variable.

    Everything looks fine on the surface, but *IF* thread 2 happened to be interrupted by a context switch back to Thread 1 after storing just the 1st two characters of the string, what would the global string now contain? It would be "Gollo World!".

    At that point, the data would be "undefined" because it was only partially updated.

    Here is another example of unsafe thread code... can you spot the problem?
    Code:
    FUNCTION MyThread(BYVAL nCount AS LONG) AS LONG
      STATIC Total AS CURRENCY
      LOCAL x AS LONG
      Total = 0
      FOR x = 1 to nCount
        Total = Total + gAccountTotals(x) ' a global array
      NEXT x
      FUNCTION = FIX(nCount)
    END FUNCTION
    Can you spot the problem?

    In fact, there are actually two problems:
    1. The content of the global array may be affected by a context switch
    2. The STATIC variable Total. If this function was executed by more than one thread, then Total would be affected by each thread causing unexpected results.

    The solutions for this simplistic code would be to change Total to a LOCAL variable, and wrap *ALL* code (that reads or writes to the global array) in a Critical Section - even code outside of the threads must get the treatment, if there is any possibility that the code could run at the same time as the thread.

    Now, if only one thread can access the array at any one time, any other thread is suspended until the first thread leaves the critical section... this enforcement can cause you to lose any performance gain that you thought threads may have given you.

    Really, this is a simplistic situation, and some of these constraints may not be applicable to your application code, but this should give you some idea of the issues you'll have to consider.

    Here endeth the lesson.

    For more information, take a look around www.fatbrain.com or www.amazon.com and search for "threaded" or maybe just "thread".

    Remember: There have been whole books written on this subject!

    I hope this helps!


    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

    Comment


    • #3
      Michael --

      Have you checked out the three articles about threads on this PowerBASIC page?

      http://www.powerbasic.com/support/technote/

      -- Eric

      ------------------
      Perfect Sync Development Tools
      Perfect Sync Web Site
      Contact Us: mailto:[email protected][email protected]</A>
      "Not my circus, not my monkeys."

      Comment


      • #4
        Hi Eric,
        I have to disagree (for once!). The examples on the forum about threading are much too difficult.
        Because you mentioned then, I looked into them again. I'm starting to really understand what they do!
        And I don't have to tell you how much trouble I've had with this issue.

        Michael, start at the beginning. Buy Rector & Newcomer 'Win32 Programming'. This will already help a lot.
        For the rest, there's a lot on the forum about the use of GlobalAllocate, passing UDT's to threads, CriticalSections, etc.
        Lance mentioned the basics which are extremely important (do yourself a favor and don't forget them; I didn't know about threading and had to rewrite quite a lot of code).
        Start building your application and build it step by step. Test all the time: build a test application to create all the threads you need of your program. If it's within your program that the threading occurs, make sure to create logfiles that use a unique number for I/O and a unique file name (based on the number).

        If there's question, be sure that the people here can help. It's quite unbelievable what I've learned so far (and I still don't know!).

        Anyway, good luck.

        PS. Eric, have you gotten the email I send (visit the web for our app?)

        ------------------

        Comment


        • #5
          > PS. Eric, have you gotten the email I send
          > (visit the web for our app?)

          That doesn't ring a bell... Please re-send.

          -- Eric


          ------------------
          Perfect Sync Development Tools
          Perfect Sync Web Site
          Contact Us: mailto:[email protected][email protected]</A>
          "Not my circus, not my monkeys."

          Comment

          Working...
          X