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. static Method And Thread Safety

static Method And Thread Safety

Scheduled Pinned Locked Moved C#
help
9 Posts 4 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.
  • M Offline
    M Offline
    M Waseem Chishti
    wrote on last edited by
    #1

    Hi guys, I am facing a problem regarding static methods, what happens when we call a static method (accessing only local variables), public static void HelpMePlease() { int a=0; a++; Console.WriteLine("A must be 1 " + " a :" + a.ToString()); a++; Console.WriteLine("A must be 2 " + " a :" + a.ToString()); a++; Console.WriteLine("A must be 3 " + " a :" + a.ToString()); a++; Console.WriteLine("A must be 4 " + " a :" + a.ToString()); } Lets suppose, I call this method from 50 threads simultaneously. will All methods keep single copy of a. or each one will have its own copy. Please explain it comprehensively, compiler internals etc. Thanks in advance M.Waseem

    L H G 3 Replies Last reply
    0
    • M M Waseem Chishti

      Hi guys, I am facing a problem regarding static methods, what happens when we call a static method (accessing only local variables), public static void HelpMePlease() { int a=0; a++; Console.WriteLine("A must be 1 " + " a :" + a.ToString()); a++; Console.WriteLine("A must be 2 " + " a :" + a.ToString()); a++; Console.WriteLine("A must be 3 " + " a :" + a.ToString()); a++; Console.WriteLine("A must be 4 " + " a :" + a.ToString()); } Lets suppose, I call this method from 50 threads simultaneously. will All methods keep single copy of a. or each one will have its own copy. Please explain it comprehensively, compiler internals etc. Thanks in advance M.Waseem

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

      M.Waseem Chishti wrote: Lets suppose, I call this method from 50 threads simultaneously. will All methods keep single copy of a. or each one will have its own copy. You already wrote the code, what does it do? On the latter part, local variables are created and placed on the stack, so each call will have its own locals. xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

      M 1 Reply Last reply
      0
      • M M Waseem Chishti

        Hi guys, I am facing a problem regarding static methods, what happens when we call a static method (accessing only local variables), public static void HelpMePlease() { int a=0; a++; Console.WriteLine("A must be 1 " + " a :" + a.ToString()); a++; Console.WriteLine("A must be 2 " + " a :" + a.ToString()); a++; Console.WriteLine("A must be 3 " + " a :" + a.ToString()); a++; Console.WriteLine("A must be 4 " + " a :" + a.ToString()); } Lets suppose, I call this method from 50 threads simultaneously. will All methods keep single copy of a. or each one will have its own copy. Please explain it comprehensively, compiler internals etc. Thanks in advance M.Waseem

        H Offline
        H Offline
        HumanOsc
        wrote on last edited by
        #3

        Hello... In other words: 1. Every Thread contains his own stack... 2. Local variables are always stored on the stack from the specified thread (each one have its own copy)... --> The static keyword before functions haves no effect on there locals... :)

        M 1 Reply Last reply
        0
        • M M Waseem Chishti

          Hi guys, I am facing a problem regarding static methods, what happens when we call a static method (accessing only local variables), public static void HelpMePlease() { int a=0; a++; Console.WriteLine("A must be 1 " + " a :" + a.ToString()); a++; Console.WriteLine("A must be 2 " + " a :" + a.ToString()); a++; Console.WriteLine("A must be 3 " + " a :" + a.ToString()); a++; Console.WriteLine("A must be 4 " + " a :" + a.ToString()); } Lets suppose, I call this method from 50 threads simultaneously. will All methods keep single copy of a. or each one will have its own copy. Please explain it comprehensively, compiler internals etc. Thanks in advance M.Waseem

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

          Here is how the IL code looks like:

          .method public hidebysig static void HelpMePlease() cil managed
          {
          // Code Size: 107 byte(s)
          .maxstack 2
          .locals (
          int32 num1)
          L_0000: ldc.i4.0
          L_0001: stloc.0
          L_0002: ldloc.0
          L_0003: ldc.i4.1
          L_0004: add
          L_0005: stloc.0
          L_0006: ldstr "A must be 1 a :"
          L_000b: ldloca.s num1
          L_000d: call instance string int32::ToString()
          L_0012: call string string::Concat(string, string)
          L_0017: call void [mscorlib]System.Console::WriteLine(string)
          L_001c: ldloc.0
          L_001d: ldc.i4.1
          L_001e: add
          L_001f: stloc.0
          L_0020: ldstr "A must be 2 a :"
          L_0025: ldloca.s num1
          L_0027: call instance string int32::ToString()
          L_002c: call string string::Concat(string, string)
          L_0031: call void [mscorlib]System.Console::WriteLine(string)
          L_0036: ldloc.0
          L_0037: ldc.i4.1
          L_0038: add
          L_0039: stloc.0
          L_003a: ldstr "A must be 3 a :"
          L_003f: ldloca.s num1
          L_0041: call instance string int32::ToString()
          L_0046: call string string::Concat(string, string)
          L_004b: call void [mscorlib]System.Console::WriteLine(string)
          L_0050: ldloc.0
          L_0051: ldc.i4.1
          L_0052: add
          L_0053: stloc.0
          L_0054: ldstr "A must be 4 a :"
          L_0059: ldloca.s num1
          L_005b: call instance string int32::ToString()
          L_0060: call string string::Concat(string, string)
          L_0065: call void [mscorlib]System.Console::WriteLine(string)
          L_006a: ret
          }

          .maxstack 2 -- this allocates space for your variable on the stack. As the local variables are allocated on the stack, every call to the method has it's own set of variables. Your function is thread safe. (As the Console class is also thread safe, even the writing to the console is thread safe. It might look awful if you have several threads writing to it at the same time, but it's safe.) --- b { font-weight: normal; }

          M 1 Reply Last reply
          0
          • L leppie

            M.Waseem Chishti wrote: Lets suppose, I call this method from 50 threads simultaneously. will All methods keep single copy of a. or each one will have its own copy. You already wrote the code, what does it do? On the latter part, local variables are created and placed on the stack, so each call will have its own locals. xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

            M Offline
            M Offline
            M Waseem Chishti
            wrote on last edited by
            #5

            yeah all methods are maintaining their own copies. all i wanted to know is what copiler does when a method is called ?? is there any diffrence between behaviour of compiler when you call a static method ??

            G 1 Reply Last reply
            0
            • H HumanOsc

              Hello... In other words: 1. Every Thread contains his own stack... 2. Local variables are always stored on the stack from the specified thread (each one have its own copy)... --> The static keyword before functions haves no effect on there locals... :)

              M Offline
              M Offline
              M Waseem Chishti
              wrote on last edited by
              #6

              Thankx :-D

              1 Reply Last reply
              0
              • G Guffa

                Here is how the IL code looks like:

                .method public hidebysig static void HelpMePlease() cil managed
                {
                // Code Size: 107 byte(s)
                .maxstack 2
                .locals (
                int32 num1)
                L_0000: ldc.i4.0
                L_0001: stloc.0
                L_0002: ldloc.0
                L_0003: ldc.i4.1
                L_0004: add
                L_0005: stloc.0
                L_0006: ldstr "A must be 1 a :"
                L_000b: ldloca.s num1
                L_000d: call instance string int32::ToString()
                L_0012: call string string::Concat(string, string)
                L_0017: call void [mscorlib]System.Console::WriteLine(string)
                L_001c: ldloc.0
                L_001d: ldc.i4.1
                L_001e: add
                L_001f: stloc.0
                L_0020: ldstr "A must be 2 a :"
                L_0025: ldloca.s num1
                L_0027: call instance string int32::ToString()
                L_002c: call string string::Concat(string, string)
                L_0031: call void [mscorlib]System.Console::WriteLine(string)
                L_0036: ldloc.0
                L_0037: ldc.i4.1
                L_0038: add
                L_0039: stloc.0
                L_003a: ldstr "A must be 3 a :"
                L_003f: ldloca.s num1
                L_0041: call instance string int32::ToString()
                L_0046: call string string::Concat(string, string)
                L_004b: call void [mscorlib]System.Console::WriteLine(string)
                L_0050: ldloc.0
                L_0051: ldc.i4.1
                L_0052: add
                L_0053: stloc.0
                L_0054: ldstr "A must be 4 a :"
                L_0059: ldloca.s num1
                L_005b: call instance string int32::ToString()
                L_0060: call string string::Concat(string, string)
                L_0065: call void [mscorlib]System.Console::WriteLine(string)
                L_006a: ret
                }

                .maxstack 2 -- this allocates space for your variable on the stack. As the local variables are allocated on the stack, every call to the method has it's own set of variables. Your function is thread safe. (As the Console class is also thread safe, even the writing to the console is thread safe. It might look awful if you have several threads writing to it at the same time, but it's safe.) --- b { font-weight: normal; }

                M Offline
                M Offline
                M Waseem Chishti
                wrote on last edited by
                #7

                is there no diffrence between how compiler treats a static method and an instance method (if both are only accessing their local variables) ??

                G 1 Reply Last reply
                0
                • M M Waseem Chishti

                  yeah all methods are maintaining their own copies. all i wanted to know is what copiler does when a method is called ?? is there any diffrence between behaviour of compiler when you call a static method ??

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

                  When you call a static method, there is no reference to the object, as there is no object. Other than that, there is no difference. Calling a static method does not use the address of an object:

                  Console.WriteLine("test");

                  ldstr "test"
                  call void [mscorlib]System.Console::WriteLine(string)

                  Calling a non-static method uses the address of the object:

                  num.ToString("d4");

                  ldloca.s num
                  ldstr "d4"
                  call instance string int32::ToString()

                  --- b { font-weight: normal; }

                  1 Reply Last reply
                  0
                  • M M Waseem Chishti

                    is there no diffrence between how compiler treats a static method and an instance method (if both are only accessing their local variables) ??

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

                    Other than sending a object reference to the instance method, no, there is no difference. --- b { font-weight: normal; }

                    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