Jump to content
  • Advertisement
Sign in to follow this  
Chicktopus

Thread Safe Friend Class Operater Overloading

This topic is 2701 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Avast!

I've been having a lot of trouble over the last couple of days with operator overloading.

Basically, I'm designing a Vector3 class and I need to be able to do things like vector1 - vector2 in any class I use them without having to expand everything out every time. The problem I'm having is that I can't get it working with my multi-threading as the functions aren't thread safe. I'll show you what I've got:

Declared in the Vector3 header:


friend Vector3& operator+(const Vector3& p_vector1, const Vector3& p_vector2);
friend Vector3& operator+(const Vector3& p_vector3f, float p_val);
friend Vector3& operator+(const float p_val, Vector3& p_vector3f);
friend Vector3& operator-(const Vector3& p_vector1, const Vector3& p_vector2);
friend Vector3& operator-(const Vector3& p_vector3f, float p_val);
friend Vector3& operator-(const float p_val, Vector3& p_vector3f);
friend Vector3& operator*(const Vector3& p_vector1, const Vector3& p_vector2);
friend Vector3& operator*(const Vector3& p_vector3f, float p_val);
friend Vector3& operator*(const float p_val, const Vector3& p_vector3f);
friend Vector3& operator/(const Vector3& p_vector1, Vector3& p_vector2);
friend Vector3& operator/(const Vector3& p_vector3f, float p_val);
friend Vector3& operator/(const float p_val, Vector3& p_vector3f);
friend Vector3& operator+=(Vector3& p_vector1, const Vector3& p_vector2);
friend Vector3& operator+=(Vector3& p_vector3f, const float p_val);
friend Vector3& operator+=(float p_val, const Vector3& p_vector3f) ;
friend Vector3& operator-=(Vector3& p_vector1, const Vector3& p_vector2);
friend Vector3& operator-=(Vector3& p_vector3f, const float p_val);
friend Vector3& operator*=(Vector3& p_vector1, const Vector3& p_vector2);
friend Vector3& operator*=(Vector3& p_vector3f, const float p_val);
friend Vector3& operator/=(Vector3& p_vector1, const Vector3& p_vector2);
friend Vector3& operator/=(Vector3& p_vector3f, const float p_val);



And as an example body:


Vector3& operator+(const Vector3& p_vector1, const Vector3& p_vector2)
{
Vector3 m_temp;
m_temp.x = p_vector1.x + p_vector2.x;
m_temp.y = p_vector1.y + p_vector2.y;
m_temp.z = p_vector1.z + p_vector2.z;
return m_temp; //<-------- Obviously not thread safe
}//Operator+



I've also tried things like:


Vector3& operator+(Vector3& p_vector1, const Vector3& p_vector2)
{
return Vector3((p_vector1.x += p_vector2.x), (p_vector1.y += p_vector2.y), (p_vector1.z += p_vector2.z));
}//Operator+


But that causes compiler error C2678: binary '+' : no operator found which takes a left-hand operand of type 'const Vector3' (or there is no acceptable conversion).

This has really been driving me nuts. Any help would be greatly appreciated!

Share this post


Link to post
Share on other sites
Advertisement
First, lets get rid of a misconception
v.x += 2;
v.y += 3;
is just as thread unsafe as
v = vector( v.x + 2, v.y + 3 );

Unless you are looking at specific compiler intrinsics, no c++ operation is atomic, not even "x += 2;".

Secondly, you shouldn't worry about your vector class being thread unsafe. You need to be insuring thread safety at a higher level. Whatever classes are touching shared vectors need to be locking before doing so.

Share this post


Link to post
Share on other sites
Thread-safety cannot be achieved to any meaningful conceptual degree at this level.

Simply put: it doesn't make sense.

Question is about "atomicity". When viewed from outside, do individual operations, such as + or add behave as if they are atomic - they cannot be interrupted in the middle.

Atomicity doesn't make much sense here either - due to composition, associativity and other rules, which part of "(a+b)*(c+b)" needs to be atomic? The additions? The multiplication? The whole equation?


The common solution to above problems is immutability and treating vectors as constant scalar types. Such solution is guaranteed to be conceptually sane, but carries considerable performance cost (see string class in .Net or Java for examples, or Haskell, Erlang for general design).


In practice, one simply doesn't worry about thread-safety inside such classes, but instead requires caller to be aware of library being thread-unaware and have them arrange data in such a way that it's not a problem.


Subtle issues, either with design or implementation performance are what keeps STM models from seeing much use with traditional (C/C++/Java/...) languages.

Share this post


Link to post
Share on other sites
Quite apart from thread-safety, the following probably shouldn't return references, but Vector3 objects by value:


friend Vector3& operator+(const Vector3& p_vector1, const Vector3& p_vector2);
friend Vector3& operator+(const Vector3& p_vector3f, float p_val);
friend Vector3& operator+(const float p_val, Vector3& p_vector3f);
friend Vector3& operator-(const Vector3& p_vector1, const Vector3& p_vector2);
friend Vector3& operator-(const Vector3& p_vector3f, float p_val);
friend Vector3& operator-(const float p_val, Vector3& p_vector3f);
friend Vector3& operator*(const Vector3& p_vector1, const Vector3& p_vector2);
friend Vector3& operator*(const Vector3& p_vector3f, float p_val);
friend Vector3& operator*(const float p_val, const Vector3& p_vector3f);
friend Vector3& operator/(const Vector3& p_vector1, Vector3& p_vector2);
friend Vector3& operator/(const Vector3& p_vector3f, float p_val);
friend Vector3& operator/(const float p_val, Vector3& p_vector3f);


It might be the case that these are currently returning references to automatic or temporary variables, which is going to cause trouble sooner or later.

Share this post


Link to post
Share on other sites

Quite apart from thread-safety, the following probably shouldn't return references, but Vector3 objects by value:

It might be the case that these are currently returning references to automatic or temporary variables, which is going to cause trouble sooner or later.


Yeah, I had the same idea, but as soon as I changed it to return by value, I started getting linker errors telling me that the functions were already defined. Really weird stuff

Share this post


Link to post
Share on other sites
Alright, sorted it. I ended up using unary overloads, which I'd tried originally but weren't working. I/m not sure what I did differently, but it seems fine, now.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!