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. MultiThread Question

MultiThread Question

Scheduled Pinned Locked Moved C / C++ / MFC
questionsharepointdata-structures
5 Posts 2 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.
  • F Offline
    F Offline
    ForNow
    wrote on last edited by
    #1

    Hi It is my understanding that local variables of a Multithread application i.e those declared on the stack for each function are thread safe. Each thread BP,SP registers have a unique address so variables declared on the stack are all unique to each thread. Am I correct in understanding this ? Thanks

    J 1 Reply Last reply
    0
    • F ForNow

      Hi It is my understanding that local variables of a Multithread application i.e those declared on the stack for each function are thread safe. Each thread BP,SP registers have a unique address so variables declared on the stack are all unique to each thread. Am I correct in understanding this ? Thanks

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      Each thread has its own stack. When calling a function from different threads, the local function variables reside on the stack of the thread from where the function has been called. So it is thread safe. But note that this does not apply to local static variables. While also local variables (can't be accessed from outside of the function), there is only one instance of such variables. Accessing them must be guarded (locks, atomic operations) and the function must not return a pointer to them to make the function thread safe.

      F 1 Reply Last reply
      0
      • J Jochen Arndt

        Each thread has its own stack. When calling a function from different threads, the local function variables reside on the stack of the thread from where the function has been called. So it is thread safe. But note that this does not apply to local static variables. While also local variables (can't be accessed from outside of the function), there is only one instance of such variables. Accessing them must be guarded (locks, atomic operations) and the function must not return a pointer to them to make the function thread safe.

        F Offline
        F Offline
        ForNow
        wrote on last edited by
        #3

        Thanks I think of static local variables as global. I am using a local variable as a switch if local variable BOOL foo = TRUE for thread A, It (BOOL foo is not the same) for thread B is there any reason in a Multithread application I would have to guard them (local variables non static ) there are no references to them by pointers Thanks

        J 1 Reply Last reply
        0
        • F ForNow

          Thanks I think of static local variables as global. I am using a local variable as a switch if local variable BOOL foo = TRUE for thread A, It (BOOL foo is not the same) for thread B is there any reason in a Multithread application I would have to guard them (local variables non static ) there are no references to them by pointers Thanks

          J Offline
          J Offline
          Jochen Arndt
          wrote on last edited by
          #4

          You only have to guard shared variables (like static variables). Variables on the stack are safe.

          F 1 Reply Last reply
          0
          • J Jochen Arndt

            You only have to guard shared variables (like static variables). Variables on the stack are safe.

            F Offline
            F Offline
            ForNow
            wrote on last edited by
            #5

            THANKS SO MUCH ;)

            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