c++ - c++11 equivalent of c# Interlocked.Increment -


i'm rewriting c# code c++:

public class lightorder {     private static int internalidcounter;      public int internalid { get; private set; }      // control myself call method once each order     public void assigninternalid(int ordersexecutorid)     {         // if internalid assigned, i.e. != 0, can print error or         internalid = interlocked.increment(ref internalidcounter);         // more     }      // more } 

this works fine - each order has sequential id if assigninternalid called different threads parallel.

what closest c++ equavalent code? should declare internalid std::atomic<int> , use ++? or should declare internalid int , use std::atomic_fetch_add?

should declare internalid std::atomic<int> , use ++?

yes.

or should declare internalid int , use std::atomic_fetch_add?

no. functions atomic_fetch_add work on atomic types (specialisations of atomic, or types atomic_int), you'd still need atomic type.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -