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. Thread synchronization problems

Thread synchronization problems

Scheduled Pinned Locked Moved C / C++ / MFC
questionlearning
5 Posts 4 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
    ss431
    wrote on last edited by
    #1

    Hi All, I am beginer for Threading program.I did some programs on all the four thread synchronization classes(CCriticalSection,CMutex,CSemaphore, CEvent). Now my question is "What happens if a thread which locks a resource crashes/terminates without unlocking it?" Thanks in Advance.

    J R M 3 Replies Last reply
    0
    • S ss431

      Hi All, I am beginer for Threading program.I did some programs on all the four thread synchronization classes(CCriticalSection,CMutex,CSemaphore, CEvent). Now my question is "What happens if a thread which locks a resource crashes/terminates without unlocking it?" Thanks in Advance.

      J Offline
      J Offline
      Jijo Raj
      wrote on last edited by
      #2

      ss431 wrote:

      "What happens if a thread which locks a resource crashes/terminates without unlocking it?"

      That resource will be locked for ever and no other threads can lock it again. Regards, Jijo.

      _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

      S 1 Reply Last reply
      0
      • J Jijo Raj

        ss431 wrote:

        "What happens if a thread which locks a resource crashes/terminates without unlocking it?"

        That resource will be locked for ever and no other threads can lock it again. Regards, Jijo.

        _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

        S Offline
        S Offline
        ss431
        wrote on last edited by
        #3

        Thank you for your reply Raj, If the resource is locked what we have to do to unlock that locked resource and make it used by other Threads. If the resource is locked by CMutex object then there is no problem as it will automatically unlocks the locked resource after the thread whick locks it is exited. But what about the situation if we use CCriticalSetion or CSemaphore,as they don't release the resources automatically.

        1 Reply Last reply
        0
        • S ss431

          Hi All, I am beginer for Threading program.I did some programs on all the four thread synchronization classes(CCriticalSection,CMutex,CSemaphore, CEvent). Now my question is "What happens if a thread which locks a resource crashes/terminates without unlocking it?" Thanks in Advance.

          R Offline
          R Offline
          Roger Stoltz
          wrote on last edited by
          #4

          ss431 wrote:

          Now my question is "What happens if a thread which locks a resource crashes/terminates without unlocking it?"

          Well, it depends what kind of resource it is and what kind of synchronization object you've used to control the access to the resource. If the resource resides only within your process, you should use a critical section (also known as a fast mutex) to synchronize access to the resource from different threads in the process. If the process crashes, you don't have a problem since the resource is gone as well. A critical section is not a kernel object and is used to synchronize resources between threads in the same process. If it's a global resource you cannot use a critical section to control access to it, you have to use a kernel object such as a mutex and give it a name (identity) in order for other processes to use the same mutex when accessing the same resource. If a process that has locked the global resource with a mutex crashes, the mutex remains locked, preventing other processes from locking it and obviously preventing other processes from using the global resource. This scenario will likely freeze your computer. Mutexes, semaphores and events are kernel objects and can be used for synchronization of access to global resources between processes. Critical sections are not kernel objects and can only be used within a single process to synchronize access to process local resources such as queues shared between threads. Reading tips: To avoid common pitfalls when starting out with multithreaded programming, read this excellent article[^]. To know how to distinguish between when synchronization can or should be avoided and when it's absolutely necessary, read this article[^]. To know how to use synchronization objects, read this article[^].

          "It's supposed to be hard, otherwise anybody could do it!" - selfquote
          "High speed never compensates for wrong direction!" - unknown

          1 Reply Last reply
          0
          • S ss431

            Hi All, I am beginer for Threading program.I did some programs on all the four thread synchronization classes(CCriticalSection,CMutex,CSemaphore, CEvent). Now my question is "What happens if a thread which locks a resource crashes/terminates without unlocking it?" Thanks in Advance.

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            In addition to Roger's reply... You should also do everything you can to make sure your threads can't crash... Handle exceptions, use good error checking, etc :) Mark

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            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