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#
  4. Pointless locking?

Pointless locking?

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

    Is it pointless to put a lock around a single line of code like this (e.g. in a 'get' accessor) regardless of how _memberString is set? lock (_locker) { return _memberString; }

    C P C 3 Replies Last reply
    0
    • M MarkLTX

      Is it pointless to put a lock around a single line of code like this (e.g. in a 'get' accessor) regardless of how _memberString is set? lock (_locker) { return _memberString; }

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      probably, because there's no changes happening to the string. However, I'd be cautious to give a definitive answer, it really depends on what else the code does. Is it multi threaded ? If not, then yes, it's a waste.

      Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

      1 Reply Last reply
      0
      • M MarkLTX

        Is it pointless to put a lock around a single line of code like this (e.g. in a 'get' accessor) regardless of how _memberString is set? lock (_locker) { return _memberString; }

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Better safe than not safe. :-D

        1 Reply Last reply
        0
        • M MarkLTX

          Is it pointless to put a lock around a single line of code like this (e.g. in a 'get' accessor) regardless of how _memberString is set? lock (_locker) { return _memberString; }

          C Offline
          C Offline
          cmk
          wrote on last edited by
          #4

          Given something like:

          class c {
          _locker;
          _memberString;
          f() {
          ...
          lock(_locker) {
          return _memberString;
          }
          }
          }
          caller() {
          c v;
          ...
          s = v.f();
          }

          In s = v.f() _memberString will need to be copied to a temp variable, or directly to s, before f returns. By locking the return you ensure that _memberString isn't modified in mid-copy ... or at least that's the way I would expect it to work in lieu of documentation to the contrary.

          ...cmk The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying. - John Carmack

          M 1 Reply Last reply
          0
          • C cmk

            Given something like:

            class c {
            _locker;
            _memberString;
            f() {
            ...
            lock(_locker) {
            return _memberString;
            }
            }
            }
            caller() {
            c v;
            ...
            s = v.f();
            }

            In s = v.f() _memberString will need to be copied to a temp variable, or directly to s, before f returns. By locking the return you ensure that _memberString isn't modified in mid-copy ... or at least that's the way I would expect it to work in lieu of documentation to the contrary.

            ...cmk The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying. - John Carmack

            M Offline
            M Offline
            MarkLTX
            wrote on last edited by
            #5

            Then the issue is CAN _memberString be modified in mid-copy, under any circumstances? Consider that... 1) Strings are immutable, at least at the level of C# code. 2) _memberString is actually a reference to a string, not the string itself. References are N bytes (can't remember what N is). Perhaps it is possible that while those N bytes are being copied, the copy could be interrupted and the N bytes overwritten by a new value, so that the reference copied to s is corrupted. Like the other guy said, better safe than sorry.

            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