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. CCriticalSection problems

CCriticalSection problems

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
2 Posts 2 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.
  • S Offline
    S Offline
    s_k
    wrote on last edited by
    #1

    Hi, I have few questions about CCriticalSection usage: 1) What is the purpose of the following code ? CSomeClass::someFunc() { . . // some code . cs.Lock(); // cs is CCriticalSection instance cs.Unlock(); . . // some code . } What's the sense ? Why it calls Lock() and immediately after it Unlock() ??? I was very surprised when I found this piece of code in MSDN... Why it isn't written this way ? CSomeClass::someFunc() { cs.Lock(); // cs is CCriticalSection instance . . . // some code . . . cs.Unlock(); } ??? 2) Is it safe to use CCriticalSection object directly, without using it together with CSingleLock ??? 3) In my program, calling CCriticalSection::Lock() sometimes blocks program execution and never returns. It just freezes the application. In which cases it happens ? Is there any way to avoid this behaviour ? Any help or suggestion will be appreciated, thanks in advance. Standa. Celebrate Mr. Cesilko!

    A 1 Reply Last reply
    0
    • S s_k

      Hi, I have few questions about CCriticalSection usage: 1) What is the purpose of the following code ? CSomeClass::someFunc() { . . // some code . cs.Lock(); // cs is CCriticalSection instance cs.Unlock(); . . // some code . } What's the sense ? Why it calls Lock() and immediately after it Unlock() ??? I was very surprised when I found this piece of code in MSDN... Why it isn't written this way ? CSomeClass::someFunc() { cs.Lock(); // cs is CCriticalSection instance . . . // some code . . . cs.Unlock(); } ??? 2) Is it safe to use CCriticalSection object directly, without using it together with CSingleLock ??? 3) In my program, calling CCriticalSection::Lock() sometimes blocks program execution and never returns. It just freezes the application. In which cases it happens ? Is there any way to avoid this behaviour ? Any help or suggestion will be appreciated, thanks in advance. Standa. Celebrate Mr. Cesilko!

      A Offline
      A Offline
      Anonymous
      wrote on last edited by
      #2

      It has been sometime since I dealt with this, so please forgive the vagness. Anyway, I think you need to declare the CS like this volatile CCriticalSection cs; otherwise the compiler will wrongly optimize the cs itself. If this does not work you may need to volatile BOOL bHasLock; and test bHasLock before Locking and Unlocking. It has been a long time, I just do not remember. But the voliatile keyword is a must to prevent the compiler optimize problems. Phil F

      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