GZGQ.COM
welcome to my space
X
Search:  
 HOME   Thread and Global Variable value
Thread and Global Variable value
Published by: jane 2009-01-08
Welcome to:gzgq.com

  • hello, something is happening and it's driving me nuts.

    i've a global variable like this:

    int iPlayerOnline;

    inside main(), when the player starts playing, y modify that variable and set it to iPlayerOnline = 1;
    Later, i start a thread (posix) with the know pthread_create(...);

    inside that thread i try to verify that value with an if statement like this

    if(iPlayerOnline==1)
    {
    ...do some stuff
    }

    but the problem is that that value is never 1, it's always 0. it never takes the value i've assigned in main(). I've read one by one every single line of the code and I NEVER assign anything to iPlayerOnline but in main when i do iPlayerOnline=1; Never 0.

    what could be happening? it has never happened to me before, it's the strangest thing i've ever seen.
    MySQL Bugs: #38108: Race condition in MyISAM backup driver::
    To force reading of state info from the index file, we changed the value of the global variable 'myisam_single_user' temporarily. If multiple threads run
    http://bugs.mysql.com/38108
    HOME
    A Programmer's Introduction to Visual Basic .NET - Google Books Result::
    href=http://books.google.com/books?id=_CBTxhBEYU4C&pg=PA245&lpg=PA245&dq=Thread+and+Global+Variable+value&source=web&ots=0lyA5fpSZl&sig=KgBi8bwYHzEJgwa327tbtXYGnKs&hl=en&sa=X&oi=book_result&resnum=6&ct=result class=l onmousedown=return clk(this.href,,,res,25,)>A Programmer's Introduction to Visual Basic .NET - Google Books Resultby Craig Utley - 2001 - Computers - 343 pagesReturning Values from Other Threads 3gO In the previous example, you most likely To see how to use global variables and check the status of the thread,
    http://books.google.com/books?id=_CBTxhBEYU4C&pg=PA245&lpg=PA245&dq=Thread+and+Global+Variable+value&source=web&ots=0lyA5fpSZl&sig=KgBi8bwYHzEJgwa327tbtXYGnKs&hl=en&sa=X&oi=book_result&resnum=6&ct=result
    HOME

    thanks!!!


  • hello, i solved it by declaring that variable like static.
    I don't know why but i read somewhere that global variables are not thread-safe so i put it like static and everthing worked fine.
    Rethinking Global Variables (Multithreaded Programming Guide ::
    Rather than return the actual error code (which could be confused with normal return values), the error code is placed into the global variable errno .
    http://docs.sun.com/app/docs/doc/801-6659/6i116aqnd?a=view
    HOME

    thanks!!!


  • You might try declaring the variable volatile, like this:

    volatile int iPlayerOnline = 0;

    That way the optimizer will "know" that it is being accessed by multiple threads and won't try any clever tricks with it.

    -Jeremy


  • It might be optimised away. It might be a bug in your code. You might go crazy. Or the bedbugs were hungry again. Hard to say without seeing your code, won't you think?

    If you're desperate you could run it through a debugger and set a breakpoint on each iPlayerOnline access. Or something.


  • I doubt that declaring the variable static fixed anything; declaring the variable static just means that it can't be referenced outside the file it was declared in. I don't see how that could effect the thread-safeness of the variable.

    One of the big problems with multithreaded programs is that they're non-deterministic. It's easy to be mislead into thinking you've "fixed" a problem when you've really just made harder to detect. The result is metastable code that works properly 999 times out of 1000 and then mysteriously malfunctions when the timing of the threads' execution is just exactly wrong. That's why when doing multithreaded programming it's really important to understand why things work (or don't work) the way they do -- you really can't rely on "it seems to be working now" the way you can with single-threaded programming.

    -Jeremy

    hello, i solved it by declaring that variable like static. I don't know why but i read somewhere that global variables are not thread-safe so i put it like static and everthing worked fine.

    thanks!!!





  • Traditional University or MLM University? You Choose
    15 Questions to Ask Your Software Vendor

    You are looking at:gzgq.com's Thread and Global Variable value, click gzgq.com to home
  • what is your biggest fear
  • why can 039 t i change the color of this image
  • is it legal for employers to put your check at minimum wage without a two week notice
  • drawing cars award worthy sites for learning how to draw cars like a pro
  • what could i say about this picture
  • can anyone help me find a font that just screams evanescence
  • do you like this picture
  • what is a full frame camera as opposed to a crop sensor camera
  • question about a log cabin
  • how can i get duct tape adhesive off hardwood floors
  • comments on my hdr pics please
  • whats the cheapest way to renovate a house diy is out of the question
  • difference between drawing and sketching
  • curtain color ideas for rustic decor
  •  
  • annie audition songs
  • do you like this photograph
  • i really help from bi help me out
  • my house has pictures hung up and i like art where there is two of the same picture but kinda different
  • how to i colour in a scanned hand drawn picture on adobe illustrator
  • looking at artists and moods for my project help
  • what is voluntary manslaughter
  • can i put a photo on the internet without breaking the law
  • in a democratic society should a felon retain the right to vote
  • will you help me with a logo for drugs
  • how can i get disney auditions
  • what is true and false
  • architecture question
  • does anyone know what these pictures are called
  •  Homepage | Add to favorites | Contact us | Exchange links | LOGIN | Site map | 
    Copyright© 2008 gzgq.com        Site made:CFZ