3

I am writing a class that needs to support both volatile and non-volatile instances (volatile instances use atomic operations, non-volatile instances use regular operations), and am wondering if I am going about it in the correct way. Here's a snippit of the class declaration so far:

class Yield {
public:
    Yield();
    Yield(Yield const &other);
    Yield(Yield const volatile &other);

    Yield &operator=(Yield const &other);
    Yield &operator=(Yield const volatile &other);

    Yield &operator+=(Yield const &other);
    Yield &operator+=(Yield const volatile &other);
    Yield volatile &operator+=(Yield const &other) volatile;
    Yield volatile &operator+=(Yield const volatile &other) volatile;

    // Other operators snipped...
};
  • Question 1: When compiling with MSVC, I get the following warning:

    warning C4521: 'util::Yield' : multiple copy constructors specified

    Does this warning portend any problems in using this class? Or can it be safely ignored?

  • Question 2: As it stands, all operators are overloaded for both a volatile and non-volatile other argument. I assume this is necessary in order to avoid slower volatile accesses for non-volatile instances? Is there an alternative to allow each method to be coded only twice (volatile lhs and non-volatile lhs) rather than 4 times (volatile and non-volatile lhs, each with volatile and non-volatile rhs)?

I hope putting these questions together is ok, otherwise please leave a comment and I can split them. Thanks!

zennehoy
  • 6,405
  • 28
  • 55

2 Answers2

3

The class has multiple copy constructors of a single type. This warning is informational; the constructors are callable in your program.

From the msdn website: Compiler Warning (level 3) C4521

Rutix
  • 861
  • 1
  • 10
  • 22
  • 3
    I editted your answer to provide the part I think you meant would answer his question. Next time its better to provide the part which would answer the question than just linking to a website. – Rutix Dec 14 '12 at 20:10
2

Volatile does not do what you think it does.

Even with VC++'s special, non-standard volatile behavior, it results in slower code than writing it properly. Use std::atomic, or if that's not available then you've probably got platform-specific barrier, fence, and atomic intrinsics. VC++ has _ReadWriteBarrier and _Interlocked functions to help you.

Community
  • 1
  • 1
Cory Nelson
  • 29,236
  • 5
  • 72
  • 110
  • Yup, I am aware that volatile doesn't automagically provide safe multithreading, I just want to use it to distinguish between needing atomic operations (when the instance is shared) and when not (the instance is only used locally). It would be nice not to have to replicate the code multiple times, but so far other variations e.g. using templates have been even uglier. – zennehoy Dec 14 '12 at 20:57
  • `volatile` doesn't provide any multithreading benfits, nor any atomicity. Consider naming your functions _atomic or something similar instead of using a `volatile` `this`, so that people reading your code can tell what it does. – Cory Nelson Dec 14 '12 at 22:35