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. Is New() inherently threadsafe?

Is New() inherently threadsafe?

Scheduled Pinned Locked Moved C#
helpquestion
4 Posts 2 Posters 4 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
    Shayne Husson
    wrote on last edited by
    #1

    I am trying to get an understanding about making classes thread-safe. Since the constructor is called just once for each instance of a class, does it follow that no other consideration needs to be given to ensure the constructor is thread-safe? Thanks for any help

    D 2 Replies Last reply
    0
    • S Shayne Husson

      I am trying to get an understanding about making classes thread-safe. Since the constructor is called just once for each instance of a class, does it follow that no other consideration needs to be given to ensure the constructor is thread-safe? Thanks for any help

      D Offline
      D Offline
      Dennis C Dietrich
      wrote on last edited by
      #2

      Shayne Husson wrote: I am trying to get an understanding about making classes thread-safe. Since the constructor is called just once for each instance of a class, does it follow that no other consideration needs to be given to ensure the constructor is thread-safe? No. However the new Operator[^] is not the problem as it (as you already mentioned) just creates an object, invokes the constructor and returns a reference to the newly created object. This will be thread-safe as long as the constructor does not access any outside object (public MyClass() {} for example is no problem at all). But let's assume you have something like this:

      public MyClass(object someObject)
      {
      // some critical operation
      }

      If you use this constructor in a multithreaded scenario you might end up having concurrent access of someObject. Now if you don't have a synchronization mechanism one thread might end up accessing someObject in an inconsistent state. So ultimately the need for synchronization in the constructor really depends on if and what you're trying to do with any objects which are known outside the constructor. Best regards Dennis

      1 Reply Last reply
      0
      • S Shayne Husson

        I am trying to get an understanding about making classes thread-safe. Since the constructor is called just once for each instance of a class, does it follow that no other consideration needs to be given to ensure the constructor is thread-safe? Thanks for any help

        D Offline
        D Offline
        Dennis C Dietrich
        wrote on last edited by
        #3

        Just a little hint I forgot to mention in my previous post: when you dynamically create instances of anything derived from Control[^] be very careful in which thread you create them (see Multithreaded Windows Forms Control Sample[^] for details). Best regards Dennis

        S 1 Reply Last reply
        0
        • D Dennis C Dietrich

          Just a little hint I forgot to mention in my previous post: when you dynamically create instances of anything derived from Control[^] be very careful in which thread you create them (see Multithreaded Windows Forms Control Sample[^] for details). Best regards Dennis

          S Offline
          S Offline
          Shayne Husson
          wrote on last edited by
          #4

          Cheers ;) that makes things clearer for me.

          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