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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Critical Sections

Critical Sections

Scheduled Pinned Locked Moved C / C++ / MFC
help
5 Posts 5 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.
  • T Offline
    T Offline
    tom groezer
    wrote on last edited by
    #1

    Is there ne problem in the code below:- Critical_Section cs; main() { EnterCriticalSection(&cs); EnterCriticalSection(&cs); LeaveCriticalSection(&cs); LeaveCriticalSection(&cs); }

    E N C D 4 Replies Last reply
    0
    • T tom groezer

      Is there ne problem in the code below:- Critical_Section cs; main() { EnterCriticalSection(&cs); EnterCriticalSection(&cs); LeaveCriticalSection(&cs); LeaveCriticalSection(&cs); }

      E Offline
      E Offline
      El Corazon
      wrote on last edited by
      #2

      Why are you entering and exitting the critical section twice? Critical sections are designed for exclusive entry into a location with threads, similar to a mutex, but the command issued to the cpu is a lock memory routine not a mutex counter resulting in slightly different behavior at the CPU level. I would have to compile to see what the cpu would think of entering and exitting twice, but I would recommend against it, and even if it "were" to work, you would have gained nothing over entering once. Use it like this: thread1() { EnterCriticalSection(&cs); // protected code here LeaveCriticalSection(&cs); } main() { EnterCriticalSection(&cs); // more protected code here LeaveCriticalSection(&cs); }

      _________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)

      1 Reply Last reply
      0
      • T tom groezer

        Is there ne problem in the code below:- Critical_Section cs; main() { EnterCriticalSection(&cs); EnterCriticalSection(&cs); LeaveCriticalSection(&cs); LeaveCriticalSection(&cs); }

        N Offline
        N Offline
        Nuxser
        wrote on last edited by
        #3

        Isn't it a deadlock type situation? I say 'deadlock type' since there will be eventually an exception raised by second EnterCriticalSection(...) as it waits and then times out because the first EnterCriticalSection(...) didn't leave yet. -- Soyuz

        1 Reply Last reply
        0
        • T tom groezer

          Is there ne problem in the code below:- Critical_Section cs; main() { EnterCriticalSection(&cs); EnterCriticalSection(&cs); LeaveCriticalSection(&cs); LeaveCriticalSection(&cs); }

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          This won't deadlock but I don't see the reason at all for entering your critical section twice. It won't deadlock because of that (from MSDN): After a thread has ownership of a critical section, it can make additional calls to EnterCriticalSection or TryEnterCriticalSection without blocking its execution. This prevents a thread from deadlocking itself while waiting for a critical section that it already owns. The thread enters the critical section each time EnterCriticalSection and TryEnterCriticalSection succeed. A thread must call LeaveCriticalSection once for each time that it entered the critical section.


          Cédric Moonen Software developer
          Charting control [v1.2]

          1 Reply Last reply
          0
          • T tom groezer

            Is there ne problem in the code below:- Critical_Section cs; main() { EnterCriticalSection(&cs); EnterCriticalSection(&cs); LeaveCriticalSection(&cs); LeaveCriticalSection(&cs); }

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            tom groezer wrote:

            Is there ne problem in the code below:-

            Such as:* No definition for Critical_Section.

            • main() does not return a value.


              "A good athlete is the result of a good and worthy opponent." - David Crow

              "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

            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