Reputation: 111
I have a function like this that uses static variable. Now i need to use it in a multi threaded application.
char *
ether_ntoa(const struct ether_addr *n)
{
static char a[18];
return (ether_ntoa_r(n, a));
}
Can i use the __thread variable instead?
char *
ether_ntoa(const struct ether_addr *n)
{
__thread char a[18];
return (ether_ntoa_r(n, a));
}
I realize i can add another argument to ether_ntoa function, but was wondering whether this will work as well?
Upvotes: 3
Views: 131
Reputation: 225227
Yes, this will work, but keep the static
specifier. From the gcc docs:
The __thread specifier may be used alone, with the extern or static specifiers, but with no other storage class specifier. When used with extern or static, __thread must appear immediately after the other storage class specifier.
The __thread specifier may be applied to any global, file-scoped static, function-scoped static, or static data member of a class. It may not be applied to block-scoped automatic or non-static data member.
Upvotes: 2