gpt4 book ai didi

__thread 可以用作局部变量吗

转载 作者:行者123 更新时间:2023-12-02 01:34:36 26 4
gpt4 key购买 nike

我有这样一个使用静态变量的函数。现在我需要在多线程应用程序中使用它。

char *
ether_ntoa(const struct ether_addr *n)
{
static char a[18];
return (ether_ntoa_r(n, a));
}

我可以改用 __thread 变量吗?

char *
ether_ntoa(const struct ether_addr *n)
{
__thread char a[18];
return (ether_ntoa_r(n, a));
}

我知道我可以向 ether_ntoa 函数添​​加另一个参数,但想知道这是否也能正常工作?

最佳答案

是的,这会起作用,但保留 static 说明符。来自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.

关于__thread 可以用作局部变量吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31951654/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com