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. making a class thread safe

making a class thread safe

Scheduled Pinned Locked Moved C / C++ / MFC
questiontestingbeta-testing
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.
  • P Offline
    P Offline
    packetlos
    wrote on last edited by
    #1

    Hi, I am trying to make my class thread safe with out much luck and wonder if anybody can point me in the right direction. An object of this class is shared between 2 threads, therefore I have added some critical sections but in testing this does not seem to be enough, below is what i have so far:void CPacket::Lock() { CSingleLock singleLock(&m_CList); singleLock.Lock(); ASSERT(singleLock.IsLocked()); } void CPacket::Unlock() { CSingleLock singleLock(&m_CList); singleLock.Unlock(); ASSERT(!singleLock.IsLocked()); }
    and in my code around all use of the object:m_capturedPacket->Lock(); m_capturedPacket->Addpacket() // do something m_capturedPacket->Unlock();
    Internally the class mainatins a CList which is being corrupted, how can I make calls to this class thread safe?

    G 1 Reply Last reply
    0
    • P packetlos

      Hi, I am trying to make my class thread safe with out much luck and wonder if anybody can point me in the right direction. An object of this class is shared between 2 threads, therefore I have added some critical sections but in testing this does not seem to be enough, below is what i have so far:void CPacket::Lock() { CSingleLock singleLock(&m_CList); singleLock.Lock(); ASSERT(singleLock.IsLocked()); } void CPacket::Unlock() { CSingleLock singleLock(&m_CList); singleLock.Unlock(); ASSERT(!singleLock.IsLocked()); }
      and in my code around all use of the object:m_capturedPacket->Lock(); m_capturedPacket->Addpacket() // do something m_capturedPacket->Unlock();
      Internally the class mainatins a CList which is being corrupted, how can I make calls to this class thread safe?

      G Offline
      G Offline
      Gary R Wheeler
      wrote on last edited by
      #2

      The CSingleLock class unlocks the object in its destructor. Your Lock() function locks the critical section, and then when the CSingleLock object is destroyed when the Lock() function returns, the critical section is unlocked. Try changing your code to this:

      void CPacket::Lock(){
      m_CList.Lock();
      ASSERT(m_CList.IsLocked());
      }
      void CPacket::Unlock(){
      m_CList.Unlock();
      ASSERT(!m_CList.IsLocked());
      }


      Software Zen: delete this;

      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