Re: thread local variable
- From: "Alex Blekhman" <tkfx.REMOVE@xxxxxxxxx>
- Date: Fri, 17 Aug 2007 12:27:00 +0300
"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
.
- Follow-Ups:
- Re: thread local variable
- From: Ulrich Eckhardt
- Re: thread local variable
- Prev by Date: If it's not UTF-16, use UTF-8, even if it's ASCII.
- Next by Date: Template member function cast bug in VC80SP1 (and more...)
- Previous by thread: Re: thread local variable
- Next by thread: Re: thread local variable
- Index(es):