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. The Lounge
  3. !!!!Static variables are not multithread!!!!!

!!!!Static variables are not multithread!!!!!

Scheduled Pinned Locked Moved The Lounge
jsontutorialquestion
21 Posts 14 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.
  • S Shog9 0

    ...the butler. Yes! The butler! The multiple threads, all of them, they came from his jacket - he did it, not me, he did it!

    Shog9

    drifting along with the tumbling tumbleweeds...

    B Offline
    B Offline
    Brian Delahunty
    wrote on last edited by
    #5

    I'm starting to worry about you now Josh. :rolleyes:* *:rolleyes: to the fact that I'm only starting now. Regards, Brian Dela :-)

    L 1 Reply Last reply
    0
    • B Brian Delahunty

      I'm starting to worry about you now Josh. :rolleyes:* *:rolleyes: to the fact that I'm only starting now. Regards, Brian Dela :-)

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

      Sorry, forgot to give him is medication last night :rolleyes: The tigress is here :-D

      J B 2 Replies Last reply
      0
      • G Gabriel 2

        Althought static variables are not recommended, I always believed there was no problemm to use them in particular situations. However I discovered Visual C implementation of STATIC VARIABLE INITIALIZATION IS NOT THREAD SAFE. I believe most programmers ignore this problemm and they programms are critically flawed. For example: void Function () { static int x = g (); ... } void Function () { static CString y; ... } Most programmers believes x and y are initialized when programm starts, but this is wrong. They are initialized on the first call to Function (). Visual C uses a static flag to check wheather the variable was initialized. There's no critical section implemented, so if this function is called by two threads at the same time, the variable may be initialized twice!!!! In the first example, function g may be called twice, what's wrong!!! The second example is even worst. The constructor for variable y may be called twice!!!! ------- Disasambly of first example ------- 16: static int i = g (); 00418F58 xor eax,eax 00418F5A mov al,[?$S11@?1??f@@YAXXZ@4EA (00438e7c)] 00418F5F and eax,1 00418F62 test eax,eax 00418F64 jne f+3Fh (00418f7f) 00418F66 mov cl,byte ptr [?$S11@?1??f@@YAXXZ@4EA (00438e7c)] 00418F6C or cl,1 00418F6F mov byte ptr [?$S11@?1??f@@YAXXZ@4EA (00438e7c)],cl 00418F75 call @ILT+0(g) (00401005) 00418F7A mov [i (00438e78)],eax 00418F7F ... --- The rest of function code ---

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

        ;P

        leppie::AllocCPArticle(Generic DFA State Machine for .NET);

        1 Reply Last reply
        0
        • L Lost User

          Sorry, forgot to give him is medication last night :rolleyes: The tigress is here :-D

          J Offline
          J Offline
          Jon Newman
          wrote on last edited by
          #8

          What have you done?


          "Póg mo thóin!"
          "One of the most important things you learn !rom the internet is that there is no ‘them’ out there. It’s just an awful lot of ‘us’."
          -Douglas Adams
          Jonathan 'nonny' Newman Homepage [www.nonny.com] [^] Bob Chum Dev site [^]

          1 Reply Last reply
          0
          • J Jeremy Falcon

            Confucius say, use correct forum, lest ye want some ancient oriental happenings on your izass! :) Jeremy Falcon

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #9

            Now, I admit I hardly come here anymore, so maybe the rules have changed, but this is not a programming question, the guy is trying to *discuss* something he has *discovered*. How is that wrong for the lounge of a programming site ? Christian NO MATTER HOW MUCH BIG IS THE WORD SIZE ,THE DATA MUCT BE TRANSPORTED INTO THE CPU. - Vinod Sharma Anonymous wrote: OK. I read a c++ book. Or...a bit of it anyway. I'm sick of that evil looking console window. I think you are a good candidate for Visual Basic. - Nemanja Trifunovic

            J N 2 Replies Last reply
            0
            • C Christian Graus

              Now, I admit I hardly come here anymore, so maybe the rules have changed, but this is not a programming question, the guy is trying to *discuss* something he has *discovered*. How is that wrong for the lounge of a programming site ? Christian NO MATTER HOW MUCH BIG IS THE WORD SIZE ,THE DATA MUCT BE TRANSPORTED INTO THE CPU. - Vinod Sharma Anonymous wrote: OK. I read a c++ book. Or...a bit of it anyway. I'm sick of that evil looking console window. I think you are a good candidate for Visual Basic. - Nemanja Trifunovic

              J Offline
              J Offline
              Jeremy Falcon
              wrote on last edited by
              #10

              Think categorically! What if someone wants to search posts for static variables, etc.? Where they gonna look? Personally, I'd choose the Visual C++ forum. Jeremy Falcon

              C 1 Reply Last reply
              0
              • J Jeremy Falcon

                Think categorically! What if someone wants to search posts for static variables, etc.? Where they gonna look? Personally, I'd choose the Visual C++ forum. Jeremy Falcon

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #11

                Think categorically ? Personally, I'd have thought some discussion on the topic and the suggestion that the guy write an article if it's not already covered somewhere would have been more helpful. Christian NO MATTER HOW MUCH BIG IS THE WORD SIZE ,THE DATA MUCT BE TRANSPORTED INTO THE CPU. - Vinod Sharma Anonymous wrote: OK. I read a c++ book. Or...a bit of it anyway. I'm sick of that evil looking console window. I think you are a good candidate for Visual Basic. - Nemanja Trifunovic

                J L K 3 Replies Last reply
                0
                • C Christian Graus

                  Think categorically ? Personally, I'd have thought some discussion on the topic and the suggestion that the guy write an article if it's not already covered somewhere would have been more helpful. Christian NO MATTER HOW MUCH BIG IS THE WORD SIZE ,THE DATA MUCT BE TRANSPORTED INTO THE CPU. - Vinod Sharma Anonymous wrote: OK. I read a c++ book. Or...a bit of it anyway. I'm sick of that evil looking console window. I think you are a good candidate for Visual Basic. - Nemanja Trifunovic

                  J Offline
                  J Offline
                  Jeremy Falcon
                  wrote on last edited by
                  #12

                  Like that can't happen in the Visual C++ forum! Also, is it just me or do you seem a little uptight? What gives? Jeremy Falcon

                  C 1 Reply Last reply
                  0
                  • J Jeremy Falcon

                    Like that can't happen in the Visual C++ forum! Also, is it just me or do you seem a little uptight? What gives? Jeremy Falcon

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #13

                    Jeremy Falcon wrote: Like that can't happen in the Visual C++ forum! I would have thought it was less likely to happen, yes. Jeremy Falcon wrote: Also, is it just me or do you seem a little uptight? What gives? Well, I have to admit that this is true. We are trying to buy a new house. I did a loan application on the phone on monday. All went well until the woman says 'now your $6,000 credit card needs to be under the limit before I can process this'. I don't HAVE a $6,000 credit card, or so I thought. My wife overheard me protesting that said card did not exist, and said 'give me the phone.' The bank rejected our application based on how my wife has handled her secret credit card. It didn't help that I couldn't call the bank again because the phone was cut off. Guess who in our house pays that bill ? Also, the shops across the road used to have a phone box, but it was removed at the shops request, because drunks used it at 2 am and made a mess of the shops. It's going back up. In front of my house, by my driveway. I've called the provider, and they basically said that they had no obligation to inform me, I can't stop them, and I should go away and let them destroy the property values of the house I'll live in for a while yet, thanks to my wifes secret credit card. So yes, I admit to being stressed and grumpy. Christian NO MATTER HOW MUCH BIG IS THE WORD SIZE ,THE DATA MUCT BE TRANSPORTED INTO THE CPU. - Vinod Sharma Anonymous wrote: OK. I read a c++ book. Or...a bit of it anyway. I'm sick of that evil looking console window. I think you are a good candidate for Visual Basic. - Nemanja Trifunovic

                    J 1 Reply Last reply
                    0
                    • C Christian Graus

                      Jeremy Falcon wrote: Like that can't happen in the Visual C++ forum! I would have thought it was less likely to happen, yes. Jeremy Falcon wrote: Also, is it just me or do you seem a little uptight? What gives? Well, I have to admit that this is true. We are trying to buy a new house. I did a loan application on the phone on monday. All went well until the woman says 'now your $6,000 credit card needs to be under the limit before I can process this'. I don't HAVE a $6,000 credit card, or so I thought. My wife overheard me protesting that said card did not exist, and said 'give me the phone.' The bank rejected our application based on how my wife has handled her secret credit card. It didn't help that I couldn't call the bank again because the phone was cut off. Guess who in our house pays that bill ? Also, the shops across the road used to have a phone box, but it was removed at the shops request, because drunks used it at 2 am and made a mess of the shops. It's going back up. In front of my house, by my driveway. I've called the provider, and they basically said that they had no obligation to inform me, I can't stop them, and I should go away and let them destroy the property values of the house I'll live in for a while yet, thanks to my wifes secret credit card. So yes, I admit to being stressed and grumpy. Christian NO MATTER HOW MUCH BIG IS THE WORD SIZE ,THE DATA MUCT BE TRANSPORTED INTO THE CPU. - Vinod Sharma Anonymous wrote: OK. I read a c++ book. Or...a bit of it anyway. I'm sick of that evil looking console window. I think you are a good candidate for Visual Basic. - Nemanja Trifunovic

                      J Offline
                      J Offline
                      Jack Handy
                      wrote on last edited by
                      #14

                      Christian Graus wrote: The bank rejected our application based on how my wife has handled her secret credit card. Christian, [sarcastic]Thanks a lot[/sarcastic] Now I need to go apologize to my wife for insisting that she was the worst money manager in the world. Hope your 'luck' turns around. -Jack

                      There are 10 types of people in this world, those that understand binary and those who don't.

                      C 1 Reply Last reply
                      0
                      • J Jack Handy

                        Christian Graus wrote: The bank rejected our application based on how my wife has handled her secret credit card. Christian, [sarcastic]Thanks a lot[/sarcastic] Now I need to go apologize to my wife for insisting that she was the worst money manager in the world. Hope your 'luck' turns around. -Jack

                        There are 10 types of people in this world, those that understand binary and those who don't.

                        C Offline
                        C Offline
                        Christian Graus
                        wrote on last edited by
                        #15

                        Jack Handy wrote: Now I need to go apologize to my wife for insisting that she was the worst money manager in the world. LOL - thanks for the first bright moment in this fiasco. Actually, we've gone to another bank and I'd run our chances at about 65% to get the house still. Either way, it's been a harrowing week. I went home on Wednesday and went to bed at 6:00. Christian NO MATTER HOW MUCH BIG IS THE WORD SIZE ,THE DATA MUCT BE TRANSPORTED INTO THE CPU. - Vinod Sharma Anonymous wrote: OK. I read a c++ book. Or...a bit of it anyway. I'm sick of that evil looking console window. I think you are a good candidate for Visual Basic. - Nemanja Trifunovic

                        1 Reply Last reply
                        0
                        • C Christian Graus

                          Think categorically ? Personally, I'd have thought some discussion on the topic and the suggestion that the guy write an article if it's not already covered somewhere would have been more helpful. Christian NO MATTER HOW MUCH BIG IS THE WORD SIZE ,THE DATA MUCT BE TRANSPORTED INTO THE CPU. - Vinod Sharma Anonymous wrote: OK. I read a c++ book. Or...a bit of it anyway. I'm sick of that evil looking console window. I think you are a good candidate for Visual Basic. - Nemanja Trifunovic

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

                          I browse the Visual C++ forum to see what people are doing and maybe even anticipate problems/solutions that I haven't dealt wit hyet. So I would suggest the Visual C++ forum is the appropriate place for the post. Elain :rose: The tigress is here :-D

                          1 Reply Last reply
                          0
                          • G Gabriel 2

                            Althought static variables are not recommended, I always believed there was no problemm to use them in particular situations. However I discovered Visual C implementation of STATIC VARIABLE INITIALIZATION IS NOT THREAD SAFE. I believe most programmers ignore this problemm and they programms are critically flawed. For example: void Function () { static int x = g (); ... } void Function () { static CString y; ... } Most programmers believes x and y are initialized when programm starts, but this is wrong. They are initialized on the first call to Function (). Visual C uses a static flag to check wheather the variable was initialized. There's no critical section implemented, so if this function is called by two threads at the same time, the variable may be initialized twice!!!! In the first example, function g may be called twice, what's wrong!!! The second example is even worst. The constructor for variable y may be called twice!!!! ------- Disasambly of first example ------- 16: static int i = g (); 00418F58 xor eax,eax 00418F5A mov al,[?$S11@?1??f@@YAXXZ@4EA (00438e7c)] 00418F5F and eax,1 00418F62 test eax,eax 00418F64 jne f+3Fh (00418f7f) 00418F66 mov cl,byte ptr [?$S11@?1??f@@YAXXZ@4EA (00438e7c)] 00418F6C or cl,1 00418F6F mov byte ptr [?$S11@?1??f@@YAXXZ@4EA (00438e7c)],cl 00418F75 call @ILT+0(g) (00401005) 00418F7A mov [i (00438e78)],eax 00418F7F ... --- The rest of function code ---

                            W Offline
                            W Offline
                            Wesner Moise
                            wrote on last edited by
                            #17

                            Oh, my god... How horrible!!! You need to be drugged. :) Thanks, Wes

                            1 Reply Last reply
                            0
                            • G Gabriel 2

                              Althought static variables are not recommended, I always believed there was no problemm to use them in particular situations. However I discovered Visual C implementation of STATIC VARIABLE INITIALIZATION IS NOT THREAD SAFE. I believe most programmers ignore this problemm and they programms are critically flawed. For example: void Function () { static int x = g (); ... } void Function () { static CString y; ... } Most programmers believes x and y are initialized when programm starts, but this is wrong. They are initialized on the first call to Function (). Visual C uses a static flag to check wheather the variable was initialized. There's no critical section implemented, so if this function is called by two threads at the same time, the variable may be initialized twice!!!! In the first example, function g may be called twice, what's wrong!!! The second example is even worst. The constructor for variable y may be called twice!!!! ------- Disasambly of first example ------- 16: static int i = g (); 00418F58 xor eax,eax 00418F5A mov al,[?$S11@?1??f@@YAXXZ@4EA (00438e7c)] 00418F5F and eax,1 00418F62 test eax,eax 00418F64 jne f+3Fh (00418f7f) 00418F66 mov cl,byte ptr [?$S11@?1??f@@YAXXZ@4EA (00438e7c)] 00418F6C or cl,1 00418F6F mov byte ptr [?$S11@?1??f@@YAXXZ@4EA (00438e7c)],cl 00418F75 call @ILT+0(g) (00401005) 00418F7A mov [i (00438e78)],eax 00418F7F ... --- The rest of function code ---

                              N Offline
                              N Offline
                              Nitron
                              wrote on last edited by
                              #18

                              If it makes you feel any better, I found out the same thing last week. X| - Nitron


                              "Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb

                              1 Reply Last reply
                              0
                              • C Christian Graus

                                Now, I admit I hardly come here anymore, so maybe the rules have changed, but this is not a programming question, the guy is trying to *discuss* something he has *discovered*. How is that wrong for the lounge of a programming site ? Christian NO MATTER HOW MUCH BIG IS THE WORD SIZE ,THE DATA MUCT BE TRANSPORTED INTO THE CPU. - Vinod Sharma Anonymous wrote: OK. I read a c++ book. Or...a bit of it anyway. I'm sick of that evil looking console window. I think you are a good candidate for Visual Basic. - Nemanja Trifunovic

                                N Offline
                                N Offline
                                Nick Parker
                                wrote on last edited by
                                #19

                                Christian Graus wrote: Now, I admit I hardly come here anymore, so maybe the rules have changed, but this is not a programming question, the guy is trying to *discuss* something he has *discovered*. How is that wrong for the lounge of a programming site ? Long time no see CG, I think some of the regulars get a little razzled when they see *any* code at all posted in the Lounge. ;) -Nick Parker

                                1 Reply Last reply
                                0
                                • C Christian Graus

                                  Think categorically ? Personally, I'd have thought some discussion on the topic and the suggestion that the guy write an article if it's not already covered somewhere would have been more helpful. Christian NO MATTER HOW MUCH BIG IS THE WORD SIZE ,THE DATA MUCT BE TRANSPORTED INTO THE CPU. - Vinod Sharma Anonymous wrote: OK. I read a c++ book. Or...a bit of it anyway. I'm sick of that evil looking console window. I think you are a good candidate for Visual Basic. - Nemanja Trifunovic

                                  K Offline
                                  K Offline
                                  Kevnar
                                  wrote on last edited by
                                  #20

                                  I think people just see code quotes and fly into an insane rage all of the sudden. Weird. These are programmers I suppose. We must tread lightly, and in the exact correct places.

                                  "Monotheism is truly a gift from the gods." - Emo Phillips

                                  1 Reply Last reply
                                  0
                                  • L Lost User

                                    Sorry, forgot to give him is medication last night :rolleyes: The tigress is here :-D

                                    B Offline
                                    B Offline
                                    Brian Delahunty
                                    wrote on last edited by
                                    #21

                                    Oh Noooooooooooo!!!!!!!!!!! Regards, Brian Dela :-)

                                    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