静态成员函数和线程安全

发布于 2024-10-10 16:17:55 字数 128 浏览 7 评论 0原文

在静态成员函数中创建的对象和变量不像在成员函数中那样被视为“本地”,因此它们现在可以在多个线程之间共享,对吗?

然而,如果您有一个创建某个对象的成员函数,那么这将是线程本地的,因此它是非共享的。

我这样说对吗?

Objects and variables created in a static member function are not considered 'local' as they would in a member function, so that they can now be shared amongst multiple threads right?

Whereas if you have a member function which creates some object, this would be local to the thread and therefore it is non-shared.

Am I correct in saying this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北座城市 2024-10-17 16:17:55

考虑这个类

class CData
{
public:
    static void func()
    {
        int a;
        static int b;
    }

    int c;
    static int d;
};

int main()
{
    CData::func();
}

现在变量 a 对于每次调用 func() 都是本地的。如果两个线程同时调用 func(),它们会获得不同版本的 a

b 是静态本地变量。该值在 func() 的不同调用之间保持不变。如果两个线程同时调用 func(),它们会访问相同版本的 b,因此它们可能需要进行同步。

c 是一个实例变量;它附加到 CData 的特定实例。 func() 无法访问 c,除非使用我将在下面展示的技巧。

d 是一个静态变量。 CData 类的所有使用之间共享一个 d 实例,因此可能需要同步。可以通过静态函数 func() 轻松使用它。

从静态函数访问实例数据的技巧是将有效对象传递到函数中。

例如

class CData
{
public:
    static void func(CData *p)
    {
        int a;
        static int b;

        b = p->c;
    }

    int c;
    static int d;
};

int main()
{
    CData data;
    CData::func(&data);
}

希望有帮助。

Consider this class

class CData
{
public:
    static void func()
    {
        int a;
        static int b;
    }

    int c;
    static int d;
};

int main()
{
    CData::func();
}

Now variable a is local to each call of func(). If two threads call func() at the same time, they get different versions of a.

b is a static local. The value persists between different calls of func(). If two threads call func() at the same time, they access the same version of b so they might need to do synchronisation.

c is an instance variable; it is attached to a particular instantiation of CData. func() cannot access c, except with a trick I'll show below.

d is a static variable. There is one instance of d shared between all uses of class CData so synchronisation may be necessary. It can be used easily from the static function func().

The trick used to access instance data from a static function is to pass a valid object into the function.

e.g.

class CData
{
public:
    static void func(CData *p)
    {
        int a;
        static int b;

        b = p->c;
    }

    int c;
    static int d;
};

int main()
{
    CData data;
    CData::func(&data);
}

Hope that helps.

绅刃 2024-10-17 16:17:55

不,你不正确。

在静态函数中创建的对象共享,任何普通函数也是如此。

如果对象本身声明为静态,则可以共享对象,并且它不取决于函数是否是静态的。

void myFunc()
{
    static MyObject o;
    o.CallMethod(); // here o is shared by all threads calling myFunc
}

当一个对象被声明为静态时,就好像该对象是一个全局变量,但仅在声明它的函数范围内可见。

No you are not correct.

Objects created in a static function are not shared, and this is also the case for any normal functions.

Objects can be shared though if they are declared static themselves, and it does not depends if the function is static or not.

void myFunc()
{
    static MyObject o;
    o.CallMethod(); // here o is shared by all threads calling myFunc
}

When an object is declared static, it is as if the object was a global variable, but only visible in the scope of the function that it is declared into.

紅太極 2024-10-17 16:17:55

不,你不正确。是的,C++ 确实过度使用了“静态”这个词。

静态类成员变量当然是全局变量,该类充当命名空间范围,并且如果它是私有的或受保护的(只能由该类访问),则具有一些访问权限差异。

然而,静态类成员函数就像常规的自由函数(不是类成员)一样,并且每次调用时都有自己的局部变量。

除了命名约定之外,静态类成员函数和常规自由函数之间的唯一真正区别在于,它可以访问类的私有成员(并且需要类的外部“实例”)。

此外,可以从具有可变模板参数的模板调用静态类成员函数,调用通常称为“编译时多态性”并且通常用于元编程。

任何函数中的静态“局部”变量都是单实例,另一方面,它也有点像全局变量,并且对线程争用问题敏感,因为调用该函数的两个线程访问同一实例。

No you are not correct. And yes, C++ does very much overuse the word "static".

A static class member variable is of course a global with the class acting as a namespace scope and with some access privilege differences if it is private or protected (can only be accessed by the class).

However a static class member function is just like a regular free-function (not class member) and has its own local variables every time it is called.

The only real difference between a static class member function and a regular free-function, apart from its naming convention, is that it has access to private members of a class (and needs an external "instance" of one).

In addition a static class member function can be called from a template with a variable template parameter, invoking what is commonly called "compile-time polymorphism" and is commonly used in meta-programming.

A static "local" variable in any function is a single-instance, on the other hand, is also a bit like a global and is sensitive to thread-contention issues as two threads calling the function access the same instance.

忘年祭陌 2024-10-17 16:17:55

函数是否静态(类方法)并不重要。只有自动变量才能被视为函数的本地。如果您知道这些数据的地址,则可以访问它。

您可以使用例如线程本地存储来将您的输出分配给专用线程上下文。

It does not matter if a function is static or not (class method). Only automatic variables can be seen as local to a function. If you have the address of those data, you may access it.

You can use e.g. thread-local storage to assign your output to a dedicated thread context.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文