 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
greg wellman Guest
|
Posted: Fri Jul 14, 2006 9:10 am Post subject: Can just reading a global be thread unsafe? |
|
|
I'm trying to track down a problem in code written by someone else.
The symptom appears to be a form of thread unsafety. Let me write a
little psuedo code
bool firsttime = true;
(some more global declarations)
AnInitFunctionThatEveryThreadRuns()
{
if (firsttime)
{
firsttime = false;
(initialize more globals)
}
}
Now here's the situation - sometimes hours after the first thread, and
many others, have been through this code, two new threads are started
pretty much simultaneously. One would think that because firsttime is
now false, that this function can't do anything so it doesn't matter
that it's technically thread unsafe. However, the globals it
initializes are changing value when these two new threads start and I
can't figure out why.
There is no other place in code that alters those globals, just a
couple that read them. Obviously the problem can't be reproduced while
debugging.
Any ideas?
TIA,
Greg |
|
| Back to top |
|
 |
Phlip Guest
|
Posted: Fri Jul 14, 2006 9:10 am Post subject: Re: Can just reading a global be thread unsafe? |
|
|
To answer your Subject line: Yes. Google "volatile".
greg wellman wrote:
| Quote: | I'm trying to track down a problem in code written by someone else.
|
And tell them to Google "global variable", to receive the scolding they so
richly deserves. It's probably on WikiPedia by now.
| Quote: | The symptom appears to be a form of thread unsafety. Let me write a
little psuedo code
bool firsttime = true;
(some more global declarations)
AnInitFunctionThatEveryThreadRuns()
{
if (firsttime)
{
firsttime = false;
(initialize more globals)
}
}
Now here's the situation - sometimes hours after the first thread, and
many others, have been through this code, two new threads are started
pretty much simultaneously. One would think that because firsttime is
now false, that this function can't do anything so it doesn't matter
that it's technically thread unsafe. However, the globals it
initializes are changing value when these two new threads start and I
can't figure out why.
There is no other place in code that alters those globals, just a
couple that read them. Obviously the problem can't be reproduced while
debugging.
|
Yep. Make the global volatile, and the threads might stop clobbering it.
Next, use a real semaphore, instead of a dumb 'bool', so the OS will support
its volatility. And ask questions about semaphores on a newsgroup covering
programming your OS kernel, because we are only qualified here to discuss
raw C++, which has no thread library.
--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!! |
|
| Back to top |
|
 |
Ian Collins Guest
|
Posted: Fri Jul 14, 2006 9:10 am Post subject: Re: Can just reading a global be thread unsafe? |
|
|
greg wellman wrote:
| Quote: | I'm trying to track down a problem in code written by someone else.
The symptom appears to be a form of thread unsafety. Let me write a
little psuedo code
bool firsttime = true;
(some more global declarations)
AnInitFunctionThatEveryThreadRuns()
{
if (firsttime)
{
firsttime = false;
(initialize more globals)
}
}
Now here's the situation - sometimes hours after the first thread, and
many others, have been through this code, two new threads are started
pretty much simultaneously. One would think that because firsttime is
now false, that this function can't do anything so it doesn't matter
that it's technically thread unsafe. However, the globals it
initializes are changing value when these two new threads start and I
can't figure out why.
There is no other place in code that alters those globals, just a
couple that read them. Obviously the problem can't be reproduced while
debugging.
OT> if you are using pthreads, use pthread_once </OT |
This sort of question is better asked on comp.programming.threads.
--
Ian Collins. |
|
| Back to top |
|
 |
Guest
|
Posted: Fri Jul 14, 2006 9:10 am Post subject: Re: Can just reading a global be thread unsafe? |
|
|
yep i would agree
steppy the gamer o_0
Free Games i love to play : <a href=http://www.gamestotal.com >massive
online games</a><a href=http://uc.gamestotal.com >free massive online
games</a><a href=http://uc.gamestotal.com/freegames/ >free strategy
games</a><a href=http://gc.gamestotal.com >strategy games</a><a
href=http://uc.gamestotal.com/i.cfm?p=about >free online strategy
games</a><a href=http://uc.gamestotal.com/i.cfm?p=aboutgc >free massive
multiplayer online games</a><a href=http://www.stephenyong.com >games
like runescape</a><a href=http://gc.gamestotal.com/i.cfm?p=aboutgc
| Quote: | multiplayer online games</a><a href=http://aw.gamestotal.com >free online mmorpg games</a><a href=http://uc.gamestotal.com >massive online games free</a><a href=http://3700.gamestotal.com >free online games like runescape</a><a href=http://www.gamestotal.com/corp/ >massive free online games</a
|
Ian Collins wrote:
| Quote: | greg wellman wrote:
I'm trying to track down a problem in code written by someone else.
The symptom appears to be a form of thread unsafety. Let me write a
little psuedo code
bool firsttime = true;
(some more global declarations)
AnInitFunctionThatEveryThreadRuns()
{
if (firsttime)
{
firsttime = false;
(initialize more globals)
}
}
Now here's the situation - sometimes hours after the first thread, and
many others, have been through this code, two new threads are started
pretty much simultaneously. One would think that because firsttime is
now false, that this function can't do anything so it doesn't matter
that it's technically thread unsafe. However, the globals it
initializes are changing value when these two new threads start and I
can't figure out why.
There is no other place in code that alters those globals, just a
couple that read them. Obviously the problem can't be reproduced while
debugging.
OT> if you are using pthreads, use pthread_once </OT
This sort of question is better asked on comp.programming.threads.
--
Ian Collins. |
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|