Announcement

Collapse
No announcement yet.

basic thread question

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

  • basic thread question

    i am trying to do some threading. i have a UDT that is an array. I pass the array index to the thread function. The thread function calls another function that uses that index as well. It uses it to access, but NOT update the UDT values.

    When i run this PB creeps along instead of pegging the CPU on my dual core as I had hoped...can a UDT not even be read by separate threads in parallel???

    thanks,
    kevin

  • #2
    Your problem has nothing to do with the fact you are running multiple threads of execution.

    Check to see if you don't have some kind of tight loop somewhere.

    OR ===> "show offending code."

    MCM
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      PB creeps along instead of pegging the CPU on my dual core as I had hoped
      Do you mean it takes forever and very little CPU? or
      Very little CPU but is done?
      Why would you want the CPU to be pegged? It makes it harder to switch windows, or cancel something or other processes if the CPU is pegged working on something in your loop.
      Engineer's Motto: If it aint broke take it apart and fix it

      "If at 1st you don't succeed... call it version 1.0"

      "Half of Programming is coding"....."The other 90% is DEBUGGING"

      "Document my code????" .... "WHYYY??? do you think they call it CODE? "

      Comment


      • #4
        You can set the Thread Priority and Process Priority to increase or decrease the amount of CPU used by the thread and/or process.
        Sincerely,

        Steve Rossell
        PowerBASIC Staff

        Comment


        • #5
          Threading Code

          Here is an example of what I am doing. I have removed most of the code and changed names to simplify matters. Basically, A thread function is being called, and it is calling another function. Array indices are being passed around into a global TYPE. When i run this non-threaded it pegs the CPU and runs fine. I am just trying to get more speed for this HTM algorithm I have written by adding threading where I can, but this screeches to halt and crawls along slowly and does not speed things up at all, which seems to indicate sometime of collision, but I can't see where it can be...

          TYPE BLAH
          OOMPH(50) as DWORD
          END TYPE

          FUNCTION PBWIN()

          DIM blah(50) as BLAH

          FOR z = 1 TO l2Start-1 'for each L1 node

          THREAD CREATE RecurseL1Nodes(z) TO hThread(z)
          SLEEP 5

          NEXT z

          DO
          FOR x1 = 1 TO l2Start-1
          SLEEP 0
          THREAD STATUS hThread(x1) TO s
          IF s <> &H103 AND s <> 0 THEN ITERATE DO
          NEXT
          LOOP WHILE s

          FOR x1 = 1 TO l2Start-1
          THREAD CLOSE hThread(x1) TO s
          NEXT x1

          END FUNCTION

          FUNCTION RecurseL1Nodes(BYVAL currentNode AS LONG) AS LONG

          LOCAL x AS LONG, y AS LONG
          LOCAL currNodeInput() AS SINGLE

          REDIM currNodeInput(%rawInpLength)

          y = LoadImageIntoNode(currentNode, currNodeInput(), L1Node(currentNode).scanNum )

          END FUNCTION


          FUNCTION LoadImageIntoNode(currentNode AS LONG, currNodeInput() AS SINGLE, z3 AS LONG) AS LONG


          imageQuandrant# = blah(currentNode).OOMPH(1)

          FUNCTION = 0

          END FUNCTION

          Comment


          • #6
            here's your tight loop...
            Code:
            DO
              FOR x1 = 1 TO l2Start-1
                 SLEEP 0
                 THREAD STATUS hThread(x1) TO s
                 IF s <> &H103 AND s <> 0 THEN ITERATE DO
              NEXT
            LOOP WHILE S
            99+% of the loop iterations here do exactly nothing because the target thread function status is %STILL_EXECUTING

            This appears to be waiting for all thread functions to complete. WaitForMultipleObjects would be a much more efficient way to do this. I have demos in the source code forum.

            FWIW, using multiple threads is not a technique to speed things up... unless you have a multi-processor system and something to force different threads of execution to run on different processors. There's "X" amount of work to be done. Either it's done serially (single-threaded) or it's done in parallel (multi-threaded). If you have to wait for "all" to get done anyway, there is no advantage to multi-threaded architecure UNLESS what has to be done involves a lot of I-O during which the computer would otherwise be doing nothing... using multiple threads of execution in this case is a good way to make use of otherwise-dead time.

            Also FWIW, although it's hard to tell with unformatted code like that , I think using that coding style you are going to have some data integrity issues unless you add some synchronization techiniques.

            To post code formatted so we can read it, see instructions here: http://www.powerbasic.com/support/pb....php?do=bbcode

            (This is the little "vB code is On" link at the bottom of each page).

            Yes, you can "EDIT" your post to insert the code tags.

            MCM
            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              After further review...

              That snippet is waiting for all thread functions to complete, but one at a time.

              MCM
              Michael Mattias
              Tal Systems (retired)
              Port Washington WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                At first glance, very confusing code.

                Why are using using threads ?

                It should be noted that one must understand fully how Threads work and how Windows treats them before using them.

                Threads do not speed up an application!

                Actually they can slow it down.

                Threads have a purpose, which is to allow multiple tasks to be done at the same time, albiet slightly slower than if the tasks were done one after the other in one thread (the primary thread).

                Windows automatically handles switching between threads, so you normally don't have to worry about it. The improper use of Sleep can cause all sorts of problems.

                I can't tell how many threads you have running (you create them in a for next loop of unknown iterations), but such thread creation could create a lot of threads if the for next loop is large. This would be very, very bad.

                You don't just create any amount of threads you want. Each thread creates over head and slows windows down.

                To illustrate:

                When using an Internet browser like IE with multiple web pages loaded (one in each tab), every new web page loaded slows things down considerably. Just because you can display multiple pages, doesn't mean IE will execute them all at full speed. IE probably uses a separate thread for each page (tab) and things can slow down quite a bit when you have too many.

                If one uses too many threads in PB app, you can create problems.

                Rule of thumb:

                If you can write the same basic code using a non-threaded model and still accomplish basically the same thing, then don't use threads.

                IMO, threads are often not used properly.

                It is easy to create them, so often it is used when they should not be.
                Chris Boss
                Computer Workshop
                Developer of "EZGUI"
                http://cwsof.com
                http://twitter.com/EZGUIProGuy

                Comment


                • #9
                  Threading

                  Hi Guys...thanks for your responses...

                  FYI..I am not a total neophyte with threading...i wrote a threaded app for genetic algorithms analyzing genetic data that worked quite well.

                  Also FYI...I am creating a mere 16 threads with this code, so it is not so much. This is a perfect application for threading, and I am running on a dual core processor, so it should definitely be faster than single threaded execution presuming I am not pegging a CPU doing something else.

                  The code I posted with the DO loop around the FOR loop is directly from the Powerbasic manual for how to code threading. So if this is not the bext way to do it, then they should update that section.

                  I still don't feel like i understyand why this is going so slow, but I'll work with that loop in particular to see if I can figure it out...

                  thanks again for all your suggestions....any more are appreciated....

                  Have a peaceful and joyous holiday...

                  Best,
                  Kevin

                  Comment


                  • #10
                    The code I posted with the DO loop around the FOR loop is directly from the Powerbasic manual for how to code threading. So if this is not the bext way to do it, then they should update that section.
                    Real Live Applications Development is a very small part of what PowerBASIC Inc. does: PB does compilers. Not surprisingly, their primary expertise is in compilers.

                    However, in this case the reason for showing it this way is probably to make it easier (a LOT easier) for new programmers to understand what is happening.

                    Be honest now, can you tell that .....
                    Code:
                    WaitForMultipleObjects nThread, BYVAL VARPTR (hThread(0)), %TRUE, %INFINITE
                    ... is doing exactly what your 'DO...FOR...NEXT..LOOP' code is doing?

                    Be even more honest... when you first started creating Windows programs, would the WMFO statement above meant anything to you, other than "Gee, I should have bought VB?"

                    MCM
                    Michael Mattias
                    Tal Systems (retired)
                    Port Washington WI USA
                    [email protected]
                    http://www.talsystems.com

                    Comment


                    • #11
                      Ok I GOTTTA ask...what is "WMFO"????

                      Also, I will be posting a similar question shortly (just to clarify things a bit and not hijack what I think was Kevin's original question)

                      Hopefully you guys will be able to explain further in there about details of comments
                      Engineer's Motto: If it aint broke take it apart and fix it

                      "If at 1st you don't succeed... call it version 1.0"

                      "Half of Programming is coding"....."The other 90% is DEBUGGING"

                      "Document my code????" .... "WHYYY??? do you think they call it CODE? "

                      Comment


                      • #12
                        Originally posted by Cliff Nichols View Post
                        Ok I GOTTTA ask...what is "WMFO"????
                        It should be "WFMO" instead of "WMFO", and with this abbreviation MCM was referring to the Windows API call "WaitForMultipleObjects" that he mentioned.

                        Kind regards
                        Eddy

                        Comment


                        • #13
                          C'mon, it's Christmas. After all, I had all the right letters.
                          Michael Mattias
                          Tal Systems (retired)
                          Port Washington WI USA
                          [email protected]
                          http://www.talsystems.com

                          Comment

                          Working...
                          X