Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.

tbb::atomic default initialisation

mgmt1969
Beginner
1,092 Views
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

0 Kudos
6 Replies
RafSchietekat
Valued Contributor III
1,092 Views
Have you read "Why atomic Has No Constructors" in the Tutorial?
0 Kudos
mgmt1969
Beginner
1,092 Views
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
0 Kudos
RafSchietekat
Valued Contributor III
1,092 Views

"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.

0 Kudos
ARCH_R_Intel
Employee
1,092 Views

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:

[cpp]struct Foo {
    atomic x;
    Foo() : x() {}
};[/cpp]

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.

0 Kudos
jefffaust
New Contributor I
1,092 Views

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

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

0 Kudos
RafSchietekat
Valued Contributor III
1,092 Views

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.

0 Kudos
Reply