I'm rewriting this c# code to c++:
public class LightOrder{ private static int internalIdCounter; public int InternalId { get; private set; } // i control myself to call this method exactly once for each order public void AssignInternalId(int ordersExecutorId) { // if InternalId is already assigned, i.e. != 0, i can print error or something InternalId = Interlocked.Increment(ref internalIdCounter); // more } // more}
This works fine - each order has sequential id even if AssignInternalId
is called from different threads parallel.
What is closest c++ equavalent to this code? Should I declare InternalId
as std::atomic<int>
and then just use ++
? Or I should declare InternalId
as int
and use something like std::atomic_fetch_add
?