Created
December 8, 2021 03:38
-
-
Save lccalluchi/fed09c6c385597e6d545e252dc633d95 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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