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/38108HOME | 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=resultHOME |
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=viewHOME |
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
|