C++ Logo

std-discussion

Advanced search

Transactional memory: please clarify

From: JUJU DUDU <acc.gccquestions_at_[hidden]>
Date: Sat, 30 Apr 2022 16:42:10 +0200
Hi,

Reading https://en.cppreference.com/w/cpp/language/transactional_memory.

My question is about atomic transactions.
I understand that *atomic_cancel* block will throw an exception if the
transaction cannot succeed.

Is it the responsibility of the caller to decide to retry in the catch
statement or is there a hidden mechanism that tries again up to the point
it succeeds?

*Eg:*

int f(){
   static int i = 0;
   atomic_noexcept {
      // other operations that may fail and throw
      ++i;
      return i;
   }}

int caller(){

unsigned k;

try{

  k = f();

}

catch(...){

  // retry here? What if that fails again and the program must keep running?

  k = f();

}

}

or should we do that this way instead?

int recursive_caller(){

unsigned retry_limit = 15;

unsigned k;

while(retry_limit--){

try{

k = f();

return k;

}
catch(...){

}

}

//handle that failure situation with an exception or else

}


As for *atomic_commit, *how can it commit if there is an exception
laying around?

An example and clarifications would be welcome.

Thanks

Received on 2022-04-30 14:42:24