Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Multithreading Oddness

Multithreading Oddness

Scheduled Pinned Locked Moved C / C++ / MFC
question
11 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Joel Holdsworth

    Hi, I'm writing a multithreaded app and I'm having some bizzare problems. I have a worker thread which has a critical section. As the application starts up, it locks a whole bunch of times, and unlocks a whole bunch more times, no problems, every single lock is followed by an unlock. Then I call Sleep, after I've called sleep, I try and lock the CCriticalSection again, and the worker thread hangs permanently. This is bizzare because I've put TRACE0s in every single function that has access to that critical section, and none of them are called while my worker is sleeping. It's like somehow my critical section is getting locked, even though nobody seems to be locking it. Does anyone have any bright ideas about how my CCriticalSection might be being locked without me realising it? Is there any good way of knowing when and who is locking my critical section? Joel Holdsworth

    J Offline
    J Offline
    Joel Holdsworth
    wrote on last edited by
    #2

    Bizzare! Replacing the CCriticalSection with a CMutex made the problem go away !?! Joel Holdsworth

    P 1 Reply Last reply
    0
    • J Joel Holdsworth

      Bizzare! Replacing the CCriticalSection with a CMutex made the problem go away !?! Joel Holdsworth

      P Offline
      P Offline
      peterchen
      wrote on last edited by
      #3

      That's weird. The main differences between critical sections and mutices is (a) mutices work cross-process, and (b) have a different timing. I suspect you still have a bug lurking in your code, that is hidden / uncovered depending on timing.


      Pandoras Gift #44: Hope. The one that keeps you on suffering.
      aber.. "Wie gesagt, der Scheiss is' Therapie"
      boost your code || Fold With Us! || sighist | doxygen

      J 1 Reply Last reply
      0
      • P peterchen

        That's weird. The main differences between critical sections and mutices is (a) mutices work cross-process, and (b) have a different timing. I suspect you still have a bug lurking in your code, that is hidden / uncovered depending on timing.


        Pandoras Gift #44: Hope. The one that keeps you on suffering.
        aber.. "Wie gesagt, der Scheiss is' Therapie"
        boost your code || Fold With Us! || sighist | doxygen

        J Offline
        J Offline
        Joel Holdsworth
        wrote on last edited by
        #4

        Agreed, however I time is not a luxury I have right now! I am seriously short of time. Joel Holdsworth

        1 Reply Last reply
        0
        • J Joel Holdsworth

          Hi, I'm writing a multithreaded app and I'm having some bizzare problems. I have a worker thread which has a critical section. As the application starts up, it locks a whole bunch of times, and unlocks a whole bunch more times, no problems, every single lock is followed by an unlock. Then I call Sleep, after I've called sleep, I try and lock the CCriticalSection again, and the worker thread hangs permanently. This is bizzare because I've put TRACE0s in every single function that has access to that critical section, and none of them are called while my worker is sleeping. It's like somehow my critical section is getting locked, even though nobody seems to be locking it. Does anyone have any bright ideas about how my CCriticalSection might be being locked without me realising it? Is there any good way of knowing when and who is locking my critical section? Joel Holdsworth

          L Offline
          L Offline
          Laffis
          wrote on last edited by
          #5

          what do u mean by sleep. You called Sleep()? That freezes the thread! You should never use that func if you want the thread to be able to process something else at the same time!

          1 Reply Last reply
          0
          • J Joel Holdsworth

            Hi, I'm writing a multithreaded app and I'm having some bizzare problems. I have a worker thread which has a critical section. As the application starts up, it locks a whole bunch of times, and unlocks a whole bunch more times, no problems, every single lock is followed by an unlock. Then I call Sleep, after I've called sleep, I try and lock the CCriticalSection again, and the worker thread hangs permanently. This is bizzare because I've put TRACE0s in every single function that has access to that critical section, and none of them are called while my worker is sleeping. It's like somehow my critical section is getting locked, even though nobody seems to be locking it. Does anyone have any bright ideas about how my CCriticalSection might be being locked without me realising it? Is there any good way of knowing when and who is locking my critical section? Joel Holdsworth

            K Offline
            K Offline
            kerrywes
            wrote on last edited by
            #6

            so let me understand this, you call each worker from your main process, then sleep in main? this s what you should do, but i suspect this is not the case... a 'good' model: while(1) { // call your threadproc sleep(L5); // watever else } dont use sleep in threadproc, this suspends execution

            J 1 Reply Last reply
            0
            • K kerrywes

              so let me understand this, you call each worker from your main process, then sleep in main? this s what you should do, but i suspect this is not the case... a 'good' model: while(1) { // call your threadproc sleep(L5); // watever else } dont use sleep in threadproc, this suspends execution

              J Offline
              J Offline
              Joel Holdsworth
              wrote on last edited by
              #7

              No No! The worker does the sleeping! The main process never stops - it just runs the message pump! Joel Holdsworth

              L K 2 Replies Last reply
              0
              • J Joel Holdsworth

                No No! The worker does the sleeping! The main process never stops - it just runs the message pump! Joel Holdsworth

                L Offline
                L Offline
                Laffis
                wrote on last edited by
                #8

                Main process and the worker thread are both threads. You call Sleep in the w-thread, it will be suspended there sleepting, and not able to receive and new message. I assume your w-thread does task when told, i.e. when getting a message to trigger execution of certain functions. If your w-thread cannot receive message, no wonder your func does not get called, let alone hit your critical section.

                S 1 Reply Last reply
                0
                • L Laffis

                  Main process and the worker thread are both threads. You call Sleep in the w-thread, it will be suspended there sleepting, and not able to receive and new message. I assume your w-thread does task when told, i.e. when getting a message to trigger execution of certain functions. If your w-thread cannot receive message, no wonder your func does not get called, let alone hit your critical section.

                  S Offline
                  S Offline
                  Steen Krogsgaard
                  wrote on last edited by
                  #9

                  Well, sleep() only sleeps for the time specified. As I understand the OP he has something like // worker thread ... sleep(sometime); myCriticalSection.Lock() // this is where the worker hangs here the sleep() call will only block the worker thread for sometime milliseconds and are thus not in itself a problem. Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

                  1 Reply Last reply
                  0
                  • J Joel Holdsworth

                    No No! The worker does the sleeping! The main process never stops - it just runs the message pump! Joel Holdsworth

                    K Offline
                    K Offline
                    kerrywes
                    wrote on last edited by
                    #10

                    thats the problem then, Sleep() suspends threads and may mess with the crit sect timing, movr Sleep() to main() and that should fix your issue. why do you sleep in worker threads anyway? do u see the oxymoron? :)

                    1 Reply Last reply
                    0
                    • J Joel Holdsworth

                      Hi, I'm writing a multithreaded app and I'm having some bizzare problems. I have a worker thread which has a critical section. As the application starts up, it locks a whole bunch of times, and unlocks a whole bunch more times, no problems, every single lock is followed by an unlock. Then I call Sleep, after I've called sleep, I try and lock the CCriticalSection again, and the worker thread hangs permanently. This is bizzare because I've put TRACE0s in every single function that has access to that critical section, and none of them are called while my worker is sleeping. It's like somehow my critical section is getting locked, even though nobody seems to be locking it. Does anyone have any bright ideas about how my CCriticalSection might be being locked without me realising it? Is there any good way of knowing when and who is locking my critical section? Joel Holdsworth

                      C Offline
                      C Offline
                      cmk
                      wrote on last edited by
                      #11

                      If, while debugging the code, the app hangs hit break (not stop). By looking at the threads you can see what each is executing. Put the CCriticalSection in the variable watch list and open it up. Inside you should find the root critical section object. One of the members of this structure is the owning thread. Look in the debugger thread list and see what that thread is doing. ...cmk Save the whales - collect the whole set

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups