Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static member functions and thread-safety

Tags:

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?

like image 343
Tony The Lion Avatar asked Jan 06 '11 16:01

Tony The Lion


People also ask

Are static functions thread safe?

Static functions are no more and no less safe than non-static ones. Being static and being thread-safe are completely orthogonal. So that said Singleton, one of the popular design pattern, is not recommended.

What are static member functions?

Static Function MembersBy declaring a function member as static, you make it independent of any particular object of the class. A static member function can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator ::.

What limitations does a static member function have?

What limitation does a static member function have? Static member functions can only access member variables that are also static.

Can we use static method in multithreading?

accessing the code is no problem, static methods can be called with multiple threads. It depends on how it is programmed in the method, if the code is not thread safe, it will cause problems.


2 Answers

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.

like image 138
Michael J Avatar answered Sep 21 '22 06:09

Michael J


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.

like image 30
Stephane Rolland Avatar answered Sep 21 '22 06:09

Stephane Rolland