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. Threading

Threading

Scheduled Pinned Locked Moved C#
question
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.
  • 1 Offline
    1 Offline
    1nsp1r3d
    wrote on last edited by
    #1

    Hi all, I always wondered, when using the lock statement, people lock other variables that the one which we want to serialize access to. Eg, if we want to serialize the call to a static int(varname++;), why do we chose to lock on another variable? Do we assume that whenever we will want to access this variable again, we're going to check if this variable has been locked previously? Thanks

    C 1 Reply Last reply
    0
    • 1 1nsp1r3d

      Hi all, I always wondered, when using the lock statement, people lock other variables that the one which we want to serialize access to. Eg, if we want to serialize the call to a static int(varname++;), why do we chose to lock on another variable? Do we assume that whenever we will want to access this variable again, we're going to check if this variable has been locked previously? Thanks

      C Offline
      C Offline
      Curtis Schlak
      wrote on last edited by
      #2

      They do it as a matter of convenience; the variables on which they lock will always exist. In C#, for example, let's say we want to access an instance variable within the class from more than one thread running within the class. So, we'd lock on the instance of the class since we know the class exists since we're running those methods! lock( this ) If we want to access a static variable within a class from more than one thread, we use the runtime information to lock on that class, since we know the class exists, since we're attempting to change the value of a static variable contained within it. lock( typeof( MyClass ) ). I hope that makes sense. "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty

      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