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. Therad safety

Therad safety

Scheduled Pinned Locked Moved C#
csharpquestionlounge
6 Posts 3 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.
  • D Offline
    D Offline
    Den2Fly
    wrote on last edited by
    #1

    Hi buddies, If more than one thread may enter .NET collections (ArrayList, NameObjectCollectionBase, ...) Add() methods should I enclose the code that calls Add() in a lock statement? Also can you tell me a general rule to underestand when I SHOULD call some method only inside a lock statement in a multi-threaded app? Thank you so much, - Mohammad --- "Art happens when you least expect it."

    G L 2 Replies Last reply
    0
    • D Den2Fly

      Hi buddies, If more than one thread may enter .NET collections (ArrayList, NameObjectCollectionBase, ...) Add() methods should I enclose the code that calls Add() in a lock statement? Also can you tell me a general rule to underestand when I SHOULD call some method only inside a lock statement in a multi-threaded app? Thank you so much, - Mohammad --- "Art happens when you least expect it."

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

      From MSDN documentation on ArrayList: "Public static members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe." As Add() is an instance member, it's not guaranteed to be thread safe, so you should use lock. The documentation should contain thread safety information for all classes. --- b { font-weight: normal; }

      D 1 Reply Last reply
      0
      • D Den2Fly

        Hi buddies, If more than one thread may enter .NET collections (ArrayList, NameObjectCollectionBase, ...) Add() methods should I enclose the code that calls Add() in a lock statement? Also can you tell me a general rule to underestand when I SHOULD call some method only inside a lock statement in a multi-threaded app? Thank you so much, - Mohammad --- "Art happens when you least expect it."

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        Look at ArrayList.Synchronized and HashTable.Synchronized xacc.ide-0.1 released! Download and screenshots

        D 2 Replies Last reply
        0
        • G Guffa

          From MSDN documentation on ArrayList: "Public static members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe." As Add() is an instance member, it's not guaranteed to be thread safe, so you should use lock. The documentation should contain thread safety information for all classes. --- b { font-weight: normal; }

          D Offline
          D Offline
          Den2Fly
          wrote on last edited by
          #4

          Yes, thank you Guffa. --- "Art happens when you least expect it."

          1 Reply Last reply
          0
          • L leppie

            Look at ArrayList.Synchronized and HashTable.Synchronized xacc.ide-0.1 released! Download and screenshots

            D Offline
            D Offline
            Den2Fly
            wrote on last edited by
            #5

            Thank you leppie --- "Art happens when you least expect it."

            1 Reply Last reply
            0
            • L leppie

              Look at ArrayList.Synchronized and HashTable.Synchronized xacc.ide-0.1 released! Download and screenshots

              D Offline
              D Offline
              Den2Fly
              wrote on last edited by
              #6

              Thank yo leppie --- "Art happens when you least expect it."

              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