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 / C++ / MFC
  4. Local static variable is thread safe?

Local static variable is thread safe?

Scheduled Pinned Locked Moved C / C++ / MFC
question
9 Posts 5 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.
  • B Offline
    B Offline
    Best Kiluyar
    wrote on last edited by
    #1

    I have such a function: template T& GetInst() { static T t; return t; } I'm not sure whether this is thread safe? If T's constructor has many operations, when two thread entered this function early and later, is there such scenario: the first thread is in T's constructor's, the second thread define t again?:)

    L M S 3 Replies Last reply
    0
    • B Best Kiluyar

      I have such a function: template T& GetInst() { static T t; return t; } I'm not sure whether this is thread safe? If T's constructor has many operations, when two thread entered this function early and later, is there such scenario: the first thread is in T's constructor's, the second thread define t again?:)

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

      c++ itself has no concept of threads. static means one and only one, not one per thread

      1 Reply Last reply
      0
      • B Best Kiluyar

        I have such a function: template T& GetInst() { static T t; return t; } I'm not sure whether this is thread safe? If T's constructor has many operations, when two thread entered this function early and later, is there such scenario: the first thread is in T's constructor's, the second thread define t again?:)

        M Offline
        M Offline
        Manoj Kumar Rai
        wrote on last edited by
        #3

        Hi, Since the value of the locat static variable is stored on heap, per my opinion it should not be thread safe.

        Manoj Never Gives up

        1 Reply Last reply
        0
        • B Best Kiluyar

          I have such a function: template T& GetInst() { static T t; return t; } I'm not sure whether this is thread safe? If T's constructor has many operations, when two thread entered this function early and later, is there such scenario: the first thread is in T's constructor's, the second thread define t again?:)

          S Offline
          S Offline
          Sameerkumar Namdeo
          wrote on last edited by
          #4

          I`m REMY wrote:

          the second thread define t again?

          I think this depends upon the Template Parameter. If first thread invokes the function with class1 type and second invokes with class2 type then those two will be entering different constructors. Since template will generate 2 copies of template class, one for class1 and other for class2.

          B 1 Reply Last reply
          0
          • S Sameerkumar Namdeo

            I`m REMY wrote:

            the second thread define t again?

            I think this depends upon the Template Parameter. If first thread invokes the function with class1 type and second invokes with class2 type then those two will be entering different constructors. Since template will generate 2 copies of template class, one for class1 and other for class2.

            B Offline
            B Offline
            Best Kiluyar
            wrote on last edited by
            #5

            I don't means different class. For example: class A { public: A(){...//some operations} }; void main() { Create_Thread(foo()); Crteae_Thread(foo()); } Is this safe?:)

            M 1 Reply Last reply
            0
            • B Best Kiluyar

              I don't means different class. For example: class A { public: A(){...//some operations} }; void main() { Create_Thread(foo()); Crteae_Thread(foo()); } Is this safe?:)

              M Offline
              M Offline
              Manoj Kumar Rai
              wrote on last edited by
              #6

              If the function foo() have a local static variable then its not thread safe.

              Manoj Never Gives up

              B 1 Reply Last reply
              0
              • M Manoj Kumar Rai

                If the function foo() have a local static variable then its not thread safe.

                Manoj Never Gives up

                B Offline
                B Offline
                Best Kiluyar
                wrote on last edited by
                #7

                So, how to make it safe? Use thread mutex: ? A& foo() { Wait_Mutex(...); static A a; Release_Mutex(...); } ?? This looks very bad performance!:)

                R 1 Reply Last reply
                0
                • B Best Kiluyar

                  So, how to make it safe? Use thread mutex: ? A& foo() { Wait_Mutex(...); static A a; Release_Mutex(...); } ?? This looks very bad performance!:)

                  R Offline
                  R Offline
                  rrrado
                  wrote on last edited by
                  #8

                  It depends on what you need. If you wish to have it shared for all threads, you need to work with it like with any other shared memory in multithreading which includes synchronisation (unless only one thread is writing the variable)


                  rrrado

                  B 1 Reply Last reply
                  0
                  • R rrrado

                    It depends on what you need. If you wish to have it shared for all threads, you need to work with it like with any other shared memory in multithreading which includes synchronisation (unless only one thread is writing the variable)


                    rrrado

                    B Offline
                    B Offline
                    Best Kiluyar
                    wrote on last edited by
                    #9

                    Of course, I want to do a shared function by all thread. However, I hope to only have one synchronization in this function. I means, the first thread entered the synchronize, all following thread return the reference of object directly.

                    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