thread safe classes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gsherp
    New Member
    • May 2007
    • 34

    thread safe classes

    How does one go about making a class that is thread safe?

    Say we created the simple animal class
    Code:
    class animal
    {            public:
                virtual void eat() const { cout << " I eat like an animal. " << endl; }
                virtual ~animal() { }
    }
    how do i go about to ensure that this class is thread safe.

    I am new to the multi-threading subject.

    Thanks.
    Last edited by pbmods; Mar 8 '09, 05:54 PM. Reason: Added CODE tags.
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    If you are not having any static members in the class and if u are not using any global variables in side the member function then you can say the class method is thread safe.

    But there may be other factors too.

    Raghu

    Comment

    • newb16
      Contributor
      • Jul 2008
      • 687

      #3
      put the cout << ... << endl; statement within critical section , as the streams are not guaranteed to be thread-safe.

      Comment

      Working...