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. why CCriticalSection::Lock doesn't allow to set the timeout

why CCriticalSection::Lock doesn't allow to set the timeout

Scheduled Pinned Locked Moved C / C++ / MFC
question
4 Posts 4 Posters 2 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.
  • R Offline
    R Offline
    Ramon F Mendes
    wrote on last edited by
    #1

    as far as I know, the only difference between CMutex and CCriticalSection is that CMutex works at the kernel level and so can be used across process boundaries So why can you specify the timeout with CMutex::Lock method, but not in CCriticalSection::Lock?

    CMutex mutex;
    mutex.Lock( 1000 );

    CCriticalSection critsec;
    critsec.Lock( 1000 ); //here you will get an assertion

    _ N C 3 Replies Last reply
    0
    • R Ramon F Mendes

      as far as I know, the only difference between CMutex and CCriticalSection is that CMutex works at the kernel level and so can be used across process boundaries So why can you specify the timeout with CMutex::Lock method, but not in CCriticalSection::Lock?

      CMutex mutex;
      mutex.Lock( 1000 );

      CCriticalSection critsec;
      critsec.Lock( 1000 ); //here you will get an assertion

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      At the API level, you can use any of the Wait functions like WaitForSingleObject to wait on a mutex but not on a critical section. That is why the MFC wrappers work in a similar fashion. For a mutex there is an associated handle. But for a critical section there is not handle.

      «_Superman_» I love work. It gives me something to do between weekends.
      Microsoft MVP (Visual C++)

      1 Reply Last reply
      0
      • R Ramon F Mendes

        as far as I know, the only difference between CMutex and CCriticalSection is that CMutex works at the kernel level and so can be used across process boundaries So why can you specify the timeout with CMutex::Lock method, but not in CCriticalSection::Lock?

        CMutex mutex;
        mutex.Lock( 1000 );

        CCriticalSection critsec;
        critsec.Lock( 1000 ); //here you will get an assertion

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

        midiway wrote:

        So why can you specify the timeout with CMutex::Lock method, but not in CCriticalSection::Lock?

        Because EnterCriticalSection() API does not accepts a timeout parameter :cool:

        nave [My Articles] [My Blog]

        1 Reply Last reply
        0
        • R Ramon F Mendes

          as far as I know, the only difference between CMutex and CCriticalSection is that CMutex works at the kernel level and so can be used across process boundaries So why can you specify the timeout with CMutex::Lock method, but not in CCriticalSection::Lock?

          CMutex mutex;
          mutex.Lock( 1000 );

          CCriticalSection critsec;
          critsec.Lock( 1000 ); //here you will get an assertion

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

          A critical section is a higher level construct than a mutex. The current critical section wraps a semeaphore it uses for locking when it has to wait. Before it waits it will spin in user mode to avoid the costly transition to kernel mode and thread context switch. You have two options to use a timeout with a critical section: 1. (Not recommended) Write a function that uses the internal LockSemaphore. You have to duplicate the functionality for EnterCriticalSection() but allow a timeout to be passed to the wait on the semaphore. 2. Spin on TryEnterCriticalSection. Use GetTickCount to check when you time out. Make sure you do this right. Read up on YieldProcessor, SwitchToThread and Sleep/SleepEx. This is only usefull if you _have to_ use a ctirical section and you _have to_ be able to timeout.

          ...cmk The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying. - John Carmack

          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