Skip to content

Instantly share code, notes, and snippets.

@lccalluchi
Created December 8, 2021 03:38
Show Gist options
  • Select an option

  • Save lccalluchi/fed09c6c385597e6d545e252dc633d95 to your computer and use it in GitHub Desktop.

Select an option

Save lccalluchi/fed09c6c385597e6d545e252dc633d95 to your computer and use it in GitHub Desktop.
#include <atomic>
#include <iostream>
#include <math.h>
using namespace std;
template <typename T>
T fetch_pow(atomic<T> &, T);
int main()
{
atomic<int> base{5};
cout << base << endl;
fetch_pow(base, 3);
cout << base << endl;
return 0;
}
template <typename T>
T fetch_pow(atomic<T> &shared, T exponent)
{
T oldValue = shared.load();
while (!shared.compare_exchange_strong(oldValue, pow(oldValue, exponent)))
;
return oldValue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment