Announcement

Collapse
No announcement yet.

Threads Locking.

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

  • Threads Locking.

    Hey there,
    thanks in advance
    here is the buzz..

    I'm calling a function from a .dll which launches various threads. Now the function that launches the threads, upon launching the threads, enters a loop that waits for all the threads to finish before the function exits. Now I think because this is such a tight loop, maybe it's caushing the whole program to freeze. How do I avoid this, someone suggested putting SLEEP in there but it didn't seem to work. Sometimes when i put a msgbox in there the threads do there thing fine and the program doesn't freeze, but this is not an alternative in the finished product. The question remains, how do I stop the program from freezing when running multiple threads and waiting for them to exit. There is no window so I cannot use dialog do events/etc.

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

  • #2
    Are you using synchronization between the threads? That is, could there be starvation or mutual-exclusion (grid-lock) issues with serialized access through a synchronzation object?

    Have you read the section in the Rector/Newcomer book on multi-threaded programming? Some of these issues are described in detail.

    Beyond this, it is fairly difficult to suggest a solution without being able to see the actual code... there could be any number of issues that could be causing problems for you.

    BTW, Ron's suggestion of using WaitForMultipleObjects() is excellent and the best way to achieve a "wait state" in your primary thread.

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

    Comment


    • #3
      Thanks guys, ron even when implimenting your wait method it still freezes. Um, your example shows different functions for each thread, eg. worker1 worker2 worker3, all the threads i launch are the same worker (worker1) could that have something to do with it? I am using two critical sections, one at the begining of the thread code, then it ends, then the main code, then another at the end that starts and finishes...

      sub thread()

      entercritical cool
      bit of code
      leavecritical cool

      main thread code

      entercritical cool2
      bit of code
      leavecritical cool2

      end sub

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

      Comment


      • #4
        See my example code under "simple job queue" in the Source code forum.

        MCM


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

        Comment


        • #5
          Come to think of it, I had a similar problem a month ago...

          If the created threads create windows, implicitly (e.g., DDE) or explicitly (CreateWindow, DialogBox/CreateDialog), and the thread function itself does not have a message loop, you get a "hang" behavior.

          So what does the thread function look like?

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

          Comment


          • #6
            Matt, can you post some code which depicts exactly how your thread function is using the critical sections. Are your critical sections initialized Globally from outside the thread function?

            The code can't exit any critical section without calling LeaveCriticalSection(), correct?

            Comment

            Working...
            X