Static functions are not thread safe in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gobi Sakthivel
    New Member
    • Jan 2013
    • 26

    Static functions are not thread safe in C++

    Hi,
    In have finished a project in C++, in that i have written a class Utils.cpp as given below
    Code:
    class Utils
    {
    public:
    static int stringToInt(std::string);
    static float stringToFloat(std::string);
    //Many more Generalized functions like this
    }
    After everything is done, while testing the project, i am getting some wired results while accessing this class functions. Later i came to know that in Multi-threaded environment these static functions are not shared, it is not safe to use static functions in multi-threaded application without synchronization in C++.

    So now anyone can suggest a solution to this. Also i have used this class extensively in my project almost in 400 places, so i cannot make these functions as normal functions and create object for this class at each place and use them. Is any one has some better solution than this? pls help me.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Bingo!

    By definition anything using a static variable/function, etc is not thread safe.

    Please read: http://bytes.com/topic/c/insights/73...obal-variables

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      A generalized function says template to me:

      static int stringToInt(std ::string);
      static float stringToFloat(s td::string);

      becomes:

      Code:
      template <class T>
      T ConvertString(std::string);
      {
         //etc...
         return T;
      }
      Use explicit specializations where needed:

      Code:
      int ConvertString(std::string)
      {
           //etc...
           return an int
      }
      float ConvertString(std::string)
      {
            //etc...
            return a float;
      }

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        It is not just static function, no function or member function in C++ is intrinsically thread safe.

        It may be that it does not need to be thread safe because the object it is a method on is only called from a single place or thread but that is very much a design decision.

        The real big question is is the function re-entrant, which is a kin to thread safe but not quite the same. A re-entrant function can be called while the flow of execution is already in it.

        Typically a function is made re-entrant by ensuring that it does not use any static or global data. That is if the only data used by the function is either parameters passed to the function or data declared on the stack in the function so that every call to the function receives a completely separate copy of the data. In this case you can safely call the function from multiple threads.

        If this is not the case you will have to make you function thread safe, the normal method would be to use a mutex to prevent the code of the function being executed more than once simultaneously. However on some micro-processor platforms disabling interrupts has the same effect (in micro-processor kernels disabling interrupts has the effect of preventing task/thread switching).

        Looking at the prototypes in your class they all appear to me to be functions that should be able to be written so that they are re-entrant so if they are causing you a problem then there is actually probably something wrong with the implementation of the function rather than its design.

        Comment

        Working...