Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local Static variable initialization is thread safe [duplicate]

Tags:

c++

c++11

c++14

Suppose I have a class with three static functions like this :

#include <vector>
#include <iostream>
using namespace std;
#include <thread>


class Employee
{

};

class client
{

public:
    void Doprocessing()
    {
        //is this thread safe in c++11/14
        static int i = CreateEmployee();

        //does it look good to use static variable like this to make it thread safe?
        static int k = ProcessEmploye();
    }

private:


    static int CreateEmployee()
    {
        static Employee * e = new Employee();
        InsertEmployee(e);
        return 1;
    }

    static int  InsertEmployee(Employee *e)
    {
        vec.push_back(e);
        return 1;
    }

    static int ProcessEmploye()
    {
        Employee* e = vec[0];
        //do something with e
        //...
        //.
        //Suppose 10 -20 lines 
        return 1;
    }


    static std::vector<Employee*> vec;
};

std::vector<Employee*> client::vec;

void func()
{
    client cobj;
    cobj.Doprocessing();
}

const int No_Of_Threads = 10;
int main() {


    std::thread * threadpointer = new std::thread[No_Of_Threads];

    std::srand(11);
    for (int i = 0; i < No_Of_Threads; i++)
    {
        threadpointer[i] = std::thread(func);
    }

    for (int i = 0; i < No_Of_Threads; i++)
    {
        threadpointer[i].join();
    }
    delete[] threadpointer;

    std::cout << " Good" << std::endl;
    return 0;
}

My questions are :
1)If I use static int i = Somefunc() and no matter how bulky somefunc is, will be called once and will it be thread safe ?
2) if answer of 1) is yes, Does it look good to programers eyes to use static int i = SomeFunc() for above purpose.

like image 698
gaurav bharadwaj Avatar asked Feb 02 '26 19:02

gaurav bharadwaj


1 Answers

Yes, it will be thread safe, but only since C++11. Static variables are initialized in thread safe way, they are often also called magic statics.

For more see here: http://en.cppreference.com/w/cpp/language/storage_duration#Static_local_variables

If multiple threads attempt to initialize the same static local variable concurrently, the initialization occurs exactly once (similar behavior can be obtained for arbitrary functions with std::call_once). Note: usual implementations of this feature use variants of the double-checked locking pattern, which reduces runtime overhead for already-initialized local statics to a single non-atomic boolean comparison.

Also - in your code you call CreateEmployee(); during initialization of static i, and CreateEmployee( also initializes a static variable. This should also be OK, you can find in standard following footnote:

The implementation must not introduce any deadlock around execution of the initializer.

As to your second question, from the code you have shown I dont see that it is OK to use static variable as a way to gain thread safety.

Are you aware that specifying a variable as static inside function body allows you to assign it only once? This means your CreateEmployee() will always return the same Employee instance.

like image 102
marcinj Avatar answered Feb 04 '26 10:02

marcinj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!