tbb::atomic default initialisation

tbb::atomic default initialisation

Hi,

It seems that tbb::atomic variables don't have default initialisation. I was expecting that they would be default initialised to zero so that if I wrap a pointer inside a tbb::atomic, for example, it would be initialised to null. The TBB book also mentions this feature. Instead, I am constantly having to write code like tbb::atomic ptr; ptr = 0;

The correction for this is trivial and I have already done so on my TBB copy but I though I would mention the issue in this forum in case I'm missing something terribly obvious.

Cheers,
manuel

7 posts / novo 0
Último post
Para obter mais informações sobre otimizações de compiladores, consulte Aviso sobre otimizações.

Have you read "Why atomic Has No Constructors" in the Tutorial?

I read the equivalent section in the TBB book. It also says that "You can rely on zero initialization to initialize an atomic to 0", which for me, under Visual, is only true if I compile in Debug mode. The compiler doesn't bother to initialise variables when in Release mode unless specifically told to.

Maybe there could be a version of tbb::atomic that is not meant to be used at file scope that would remove this restriction and make initialisation more natural.

Cheers,
manuel

"The compiler doesn't bother to initialise variables when in Release mode unless specifically told to."
Then it would not be implementing standard C++. Are you quite sure?

"Maybe there could be a version of tbb::atomic that is not meant to be used at file scope that would remove this restriction and make initialisation more natural."
It's a tricky issue. Not providing such a constructor probably helps avoid mistakes, but the last version of C++0x I've looked at does have them (along with a lot of other stuff I don't agree with), although I haven't verified whether the compiler does anything special there.

An atomic that is a local variable will not be zero initialized.

However, usually an instance of atomic is at file scope or a class member. To force zero initialization of it as a class member, mention it explicitly in the member initialization list. For example:

struct Foo {
    atomic x;
    Foo() : x() {}
};

The x() in the member initialization list forces x to be zero initialized. This feature of C++ was originally motivated by template writers who needed a way to initialize a member to a deterministic value, where the member might be instantiated as a user-defined type or a built-in type.

struct Foo { tbb::atomic<double> x; Foo() : x() {} };

This does not work in Visual Studio 2010.  The double value in x is not initialized.

That's not good...

How about x(tbb::make_atomic<double>(0.0))? Or simply x(0.0) (only with C++11)?

Debug and/or release?

double and/or int?

What's the TBB version?

(Added 2013-10-29) TestValueInitialization in src/test/test_atomic.cpp is supposed to verify this (although testing all bytes would be better than just the first one), so this seems very strange indeed. Perhaps the test should be extended to also specifically verify member variable initialisation if this pans out, to guard against a misbehaving compiler.

Deixar um comentário

Faça login para adicionar um comentário. Não é membro? Inscreva-se hoje mesmo!