Re: thread local variable



"bangwo" wrote:
to make the nextBuf and workBuf in the following code multi-thread save,
is it correct to replace "static" with "__declspec(thread)" in the code?
or it should be "__declspec(thread) static" ? THANKS FOR THE HELP!
------
static int nextBuf ;
static char workBuf[8][64] ;
char * GetBuf()
{
nextBuf++ ;
if( nextBuf >= 8 ) nextBuf = 0 ;
return workBuf[nextBuf] ;
}

In addition to Mark's answer. `__declspec(thread)' modifier can be applied to static variables only.

"__declspec(thread)"
http://msdn2.microsoft.com/en-us/library/9w1sdazb(vs.80).aspx

<quote>
You must observe these guidelines when declaring thread local objects and variables:
....
* You can specify the thread attribute only on data items with static storage duration. This includes global data objects (both static and extern), local static objects, and static data members of classes. You cannot declare automatic data objects with the thread attribute.
</quote>

Alex

.