I was looking for thread safe unique id generation in c++ application life, So I have created following static class.
class Utils {
public :
static int getUid();
};
int Utils::getUid() {
static std::atomic<std::uint32_t> uid;
uid = 0;
return ++uid;
}
Any suggestion, Is this right approach.
fix it this way:
class Utils {
public :
static int getUid();
};
int Utils::getUid() {
static std::atomic<std::uint32_t> uid { 0 }; // <<== initialised
// uid = 0; <<== removed
return ++uid;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With