Using static mutex in a class

Posted by Dmitry Yudakov on Stack Overflow See other posts from Stack Overflow or by Dmitry Yudakov
Published on 2010-03-17T14:21:41Z Indexed on 2010/03/17 14:31 UTC
Read the original article Hit count: 265

Filed under:
|
|
|

I have a class that I can have many instances of. Inside it creates and initializes some members from a 3rd party library (that use some global variables) and is not thread-safe.

I thought about using static boost::mutex, that would be locked in my class constructor and destructor. Thus creating and destroying instances among my threads would be safe for the 3rd party members.



class MyClass

{

  static boost::mutex mx;



  // 3rd party library members

public:

  MyClass();

  ~MyClass();

};



MyClass::MyClass()

{

  boost::mutex::scoped_lock scoped_lock(mx);

  // create and init 3rd party library stuff

}

MyClass::~MyClass()

{

  boost::mutex::scoped_lock scoped_lock(mx);

  // destroy 3rd party library stuff

}




I cannot link because I receive error:

undefined reference to `MyClass::mx`
  1. Do I need some special initialization of such static member?

  2. Is the whole conception of static mutex wrong?

© Stack Overflow or respective owner

Related posts about c++

Related posts about boost