Skip to content

Increment by N, return previous

Atomically adds the given value to the current value and returns the previous value of the counter. It is possible that multiple concurrent client requests may receive the same previous value. This occurs since only the incrementing part of the logic is atomic, the retrieval of the value before it is incremented is not.

// *******************************************
// synchronous methods
// *******************************************

// Backendless.Counters approach
public int Backendless.Counters.GetAndAdd( String counterName, Int64 value );
public T Backendless.Counters.GetAndAdd<T>( String counterName, Int64 value );

// IAtomic approach
IAtomic<T> counter = Backendless.Counters.Of<T>( String counterName );
public T counter.GetAndAdd( Int64 value );

// *******************************************
// asynchronous methods
// *******************************************

// Backendless.Counters approach
public void Backendless.Counters.GetAndAdd<T>( String counterName, Int64 value, AsyncCallback<T> callback );

// IAtomic approach
IAtomic<T> counter = Backendless.Counters.Of<T>( String counterName );
public void counter.GetAndAdd( Int64 value, AsyncCallback<T> callback );

where:

Argument                Description
counterName name of the counter to increment.
value number to add to the current counter value
T numeric type for the counter values. Can be an instance of byte, short, int, float, long and double.
callback the callback used for asynchronous calls to indicate that the operation has either successfully completed or resulted in error.

Example

AsyncCallback<int> callback = new AsyncCallback<int>(
  result =>
  {
    System.Console.WriteLine( "[ASYNC] previous counter value is - " + result );
  },
  fault =>
  {
     System.Console.WriteLine( "Error - " + fault );
  } );

Backendless.Counters.GetAndAdd( "my counter", 1000, callback );

IAtomic<int> myCounter = Backendless.Counters.Of<int>( "my counter" );
int counter = myCounter.GetAndAdd( 1000 );
System.Console.WriteLine( "[SYNC] previous counter value through IAtomic is - " + counter );

counter = Backendless.Counters.GetAndAdd( "my counter", 1000 );
System.Console.WriteLine( "[SYNC] previous counter value is - " + counter );

long longCounter = Backendless.Counters.GetAndAdd<long>( "my counter", 1000 );
System.Console.WriteLine( "[SYNC] previous counter value is - " + longCounter );

Codeless Reference

codeless_atomic_counters_increment_1_return_previous

where:

Argument                Description
counter name Name of the counter whose value must be incremented.
increment by Specify the number to increment the value by.
return previous value When this box is checked, the operation returns the previous value of the counter.

Returns the previous value of the counter.

Consider the following counter:

codeless_atomic_counters_increment_1_return_previous_2

The example below increments the value of the "CarsParkingLot" by 20. This operation returns 0, since the return previous value box is checked. To avoid confusion, the value was  incremented by 20, even though the return value is 0.

codeless_atomic_counters_increment_n_return_previous_2

The result of this operation will look as shown below after the Codeless logic runs:

codeless_atomic_counters_increment_n_return_previous_3