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#
  4. C# Monitors [modified]

C# Monitors [modified]

Scheduled Pinned Locked Moved C#
csharpquestion
3 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.
  • M Offline
    M Offline
    manustone
    wrote on last edited by
    #1

    Hi all I'm new to C# multithreading and I read that to resolve deadlock it is best to use monitor with timeout.With this in mind I used the following code to see what happens to the deadlocking generated. The result is the following exception Unhandled Exception: System.Threading.SynchronizationLockException: Object synchronization method was called from an unsynchronized block of code. Why this? What's wrong with my code or my approach? Regards manuStone class ThreadFoos { public static void First() { Monitor.TryEnter(ResourceA, 1000); try{ Thread.Sleep(500); Monitor.TryEnter(ResourceB, 1000); try{ Console.WriteLine("First"); } finally{ Monitor.Exit(ResourceB); } } finally{ Monitor.Exit(ResourceA); } } public static void Second() { Monitor.TryEnter(ResourceB, 1000); try{ Thread.Sleep(500); Monitor.TryEnter(ResourceA, 1000); try{ Console.WriteLine("Second"); } finally{ Monitor.Exit(ResourceA); } } finally{ Monitor.Exit(ResourceB); } } private static object ResourceA = new Object(); private static object ResourceB = new Object(); }

    G 1 Reply Last reply
    0
    • M manustone

      Hi all I'm new to C# multithreading and I read that to resolve deadlock it is best to use monitor with timeout.With this in mind I used the following code to see what happens to the deadlocking generated. The result is the following exception Unhandled Exception: System.Threading.SynchronizationLockException: Object synchronization method was called from an unsynchronized block of code. Why this? What's wrong with my code or my approach? Regards manuStone class ThreadFoos { public static void First() { Monitor.TryEnter(ResourceA, 1000); try{ Thread.Sleep(500); Monitor.TryEnter(ResourceB, 1000); try{ Console.WriteLine("First"); } finally{ Monitor.Exit(ResourceB); } } finally{ Monitor.Exit(ResourceA); } } public static void Second() { Monitor.TryEnter(ResourceB, 1000); try{ Thread.Sleep(500); Monitor.TryEnter(ResourceA, 1000); try{ Console.WriteLine("Second"); } finally{ Monitor.Exit(ResourceA); } } finally{ Monitor.Exit(ResourceB); } } private static object ResourceA = new Object(); private static object ResourceB = new Object(); }

      G Offline
      G Offline
      gnadeem
      wrote on last edited by
      #2

      hi there. this is what i think. to start with http://msdn2.microsoft.com/en-us/library/system.threading.monitor.exit.aspx[^] which implies that Monitor.Exit can be called on a thread , for a resource only if that thread has already acquired lock for that resource. you are calling Monitor.Exit (one out of try statements) on threads without assuring that a thread has already successfully acquired monitor on a specified resource. (i.e Monitor.TryEnter may not have been successfull). please Check the returned value from Monitor.TryEnter for both 'First' and 'Second' rountines. regards.

      M 1 Reply Last reply
      0
      • G gnadeem

        hi there. this is what i think. to start with http://msdn2.microsoft.com/en-us/library/system.threading.monitor.exit.aspx[^] which implies that Monitor.Exit can be called on a thread , for a resource only if that thread has already acquired lock for that resource. you are calling Monitor.Exit (one out of try statements) on threads without assuring that a thread has already successfully acquired monitor on a specified resource. (i.e Monitor.TryEnter may not have been successfull). please Check the returned value from Monitor.TryEnter for both 'First' and 'Second' rountines. regards.

        M Offline
        M Offline
        manustone
        wrote on last edited by
        #3

        Thank you very much for your resposne. Have a nice day. Regards manuStone

        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