Consistent, clear sequences in C ++ 11 using Atomic

I have two questions about atomism:

1) Is the following code a guarantee of the return of consecutive monotonically increasing sequences without duplicates in a multi-threaded setup?

#include <atomic>

struct AtomicCounter {
    std::atomic<int> value;

    AtomicCounter() : value( 0 ) {}

    int getNextSequence(){
    return ++value;
    }
};

2) Is there an easier way to initialize? None of this succeeded:

std::atomic<int> value ( 0 ) ;
std::atomic<int> value { 0 } ;
std::atomic<int> value=0;

Thanks in advance

+4
source share
2 answers
  • , , . " " - , , , , , . , , , , .
  • atomic<int> value {0}; - (). ++ ++ 11, ++ 98 .

++ 11, .

+9
  • , .

  • , , .

0

Source: https://habr.com/ru/post/1539941/


All Articles