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. Trying to design thread safe class

Trying to design thread safe class

Scheduled Pinned Locked Moved C#
designbeta-testingfunctionalhelpquestion
31 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.
  • J John Torjo

    1. Yes it is - variables X1/X2 are automatically initialized to zero. 2. You modified your code:

    public static int X1()
    {
    if(!isInited()) throw new Exception("init first");
    return x1;

    }

    should be

    public static int X1()
    {
    lock(this) {
    if(!isInited()) throw new Exception("init first");
    return x1;
    }
    }

    Best, John

    -- LogWizard - Log Viewing can be a joy!

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #19

    Yeah but I said what if I do

    public static int X1()
    {

      return x1;
    

    }

    Granted none calls these methods before INIT method is called (I can ensure this because I am writing those methods) - I will check isInited calls in the start of each method calling this class

    J 1 Reply Last reply
    0
    • L Lost User

      Yeah but I said what if I do

      public static int X1()
      {

        return x1;
      

      }

      Granted none calls these methods before INIT method is called (I can ensure this because I am writing those methods) - I will check isInited calls in the start of each method calling this class

      J Offline
      J Offline
      John Torjo
      wrote on last edited by
      #20

      No, that is not thread-safe. This is :

      public static int X1{} { lock(locker) return x1; }

      Best, John

      -- Romania is in mourning

      L 1 Reply Last reply
      0
      • J John Torjo

        No, that is not thread-safe. This is :

        public static int X1{} { lock(locker) return x1; }

        Best, John

        -- Romania is in mourning

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #21

        Please look at my final version and tell me what you think: http://codepad.org/T3D6uabs[^]

        J 1 Reply Last reply
        0
        • L Lost User

          Please look at my final version and tell me what you think: http://codepad.org/T3D6uabs[^]

          J Offline
          J Offline
          John Torjo
          wrote on last edited by
          #22

          yes, it's threadsafe

          -- LogWizard - Log Viewing can be a joy!

          L 1 Reply Last reply
          0
          • J John Torjo

            yes, it's threadsafe

            -- LogWizard - Log Viewing can be a joy!

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #23

            Thanks a lot, few more questions, and I would appreciate if you can help: - if I add more variables other than x1 and x2 but I maintain same style in accessing and initializing them will it affect thread safety? will it affect performance? (probably). Do you know more performance friendly way to solve the problem my class solves?

            J 1 Reply Last reply
            0
            • L Lost User

              Thanks a lot, few more questions, and I would appreciate if you can help: - if I add more variables other than x1 and x2 but I maintain same style in accessing and initializing them will it affect thread safety? will it affect performance? (probably). Do you know more performance friendly way to solve the problem my class solves?

              J Offline
              J Offline
              John Torjo
              wrote on last edited by
              #24

              1. if you add more variables in the same manner, yes 2. will it affect performance? don't worry about that, performance will be the same. And on the init()/deinit() - the "decrease" won't be noticeable. 3. Your class is fine Best, John

              -- LogWizard - Log Viewing can be a joy!

              L B 2 Replies Last reply
              0
              • J John Torjo

                1. if you add more variables in the same manner, yes 2. will it affect performance? don't worry about that, performance will be the same. And on the init()/deinit() - the "decrease" won't be noticeable. 3. Your class is fine Best, John

                -- LogWizard - Log Viewing can be a joy!

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #25

                Thanks a lot!!! So you are saying if I add more properties in same manner, it won't be problem?

                J 1 Reply Last reply
                0
                • J John Torjo

                  1. if you add more variables in the same manner, yes 2. will it affect performance? don't worry about that, performance will be the same. And on the init()/deinit() - the "decrease" won't be noticeable. 3. Your class is fine Best, John

                  -- LogWizard - Log Viewing can be a joy!

                  B Offline
                  B Offline
                  BillWoodruff
                  wrote on last edited by
                  #26

                  Great job supporting this OP, John. I am voting your replies up. cheers, Bill

                  «I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.

                  J L 2 Replies Last reply
                  0
                  • L Lost User

                    Thanks a lot!!! So you are saying if I add more properties in same manner, it won't be problem?

                    J Offline
                    J Offline
                    John Torjo
                    wrote on last edited by
                    #27

                    Yup, that's exactly what I'm saying :) Best, John

                    -- LogWizard - Log Viewing can be a joy!

                    L 1 Reply Last reply
                    0
                    • B BillWoodruff

                      Great job supporting this OP, John. I am voting your replies up. cheers, Bill

                      «I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.

                      J Offline
                      J Offline
                      John Torjo
                      wrote on last edited by
                      #28

                      Thanks :-O

                      -- LogWizard - Log Viewing can be a joy!

                      1 Reply Last reply
                      0
                      • J John Torjo

                        Yup, that's exactly what I'm saying :) Best, John

                        -- LogWizard - Log Viewing can be a joy!

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #29

                        Thanks for help

                        J 1 Reply Last reply
                        0
                        • B BillWoodruff

                          Great job supporting this OP, John. I am voting your replies up. cheers, Bill

                          «I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #30

                          I am voting your replies up. me too

                          1 Reply Last reply
                          0
                          • L Lost User

                            Thanks for help

                            J Offline
                            J Offline
                            John Torjo
                            wrote on last edited by
                            #31

                            you're welcome Best, John

                            -- LogWizard - Log Viewing can be a joy!

                            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