Re: thread local variable
Replied below...
"bangwo" <bangwo@discussions.microsoft.com> wrote in message
news:A1502DD2-848A-44F2-A121-A369FBFA2143@microsoft.com...
first, THANKS FOR ALL YOUR HELP!
here is a piece of original code in a DLL where the GetBuf() comes from:
//---fmt.h
class AFX_EXT_CLASS Fmt
{
public:
static const char * CommaNum( DWORD n ) ; // n to 999,999 (W/commas)
static const char * ....
};
//---fmt.cpp
static int nextBuf ;
static char workBuf[8][64] ;
char * GetBuf()
{
nextBuf++ ;
if( nextBuf >= 8 ) nextBuf = 0 ;
return workBuf[nextBuf] ;
}
const char * Fmt::CommaNum( DWORD n )
{
char * str = GetBuf() ;
if( n >= 1000000000 )
{
sprintf( str , "%u,%03u,%03u,%03u" , n/1000000000 , n/1000000%1000
,
n/1000%1000 , n%1000 ) ;
}
else if( n >= 1000000 ) {
sprintf( str , "%u,%03u,%03u" , n/1000000 , n/1000%1000 , n%1000 )
;
} else if( n >= 1000 ) {
sprintf( str , "%u,%03u" , n / 1000 , n % 1000 ) ;
} else {
sprintf( str , "%u" , n ) ;
}
return str ;
}
------------------
the Fmt::xxxx functions are called by many threads.
so the following GetBuf can fix the problem that two threads could wind up
sharing a buffer:
char * GetBuf()
{
__declspec(thread) static int nextBuf=0 ;
__declspec(thread) static char workBuf[8][64] ;
if( nextBuf >= 8 ) nextBuf = 0 ;
return (workBuf[nextBuf++]) ;
}
-- correct?
1.
can I simply remove this GetBuf(), and replace
"char*str = GetBuf()" with " __declspec(thread) char str[64] ;" in each
Fmt::xxxx function ?
2.
(a) __declspec(thread) int i ;
(b) int __declspec(thread) i ;
-- are (a) and (b) mean the same?
For 2, they are the same.
For 1, it still depends on what you need.
If you want one nextBuf/workBuf shared between all threads, then omit the
__declspec(thread).
If you want each thread to have its own nextBuf/workBuf, then use
__declspec(thread).
Does every thread have its own Fmt object or is one Fmt object shared
between all threads?
Do the nextBuf/workBuf variables really need to be at file/global scope?
I'm just wondering, since you're using C++ and have classes, if you can
simplify the design :)
Mark