I have the following singleton class:
class RecordKeeper {
public:
...
RecordKeeper* get();
void addRecord(std::string &rec);
private:
std::string records[MAX]; // this has to be a fixed size circular array
int len;
};
void RecordKeeper::addRecord(std::string rec) {
...
records[len++] = rec;
...
}
I want to do the following from multiple threads:
RecordKeeper::get()->addRecord(str);
Can I do it as an atomic operation using std::atomic
?
Currently I am using std::mutex and lock_guard like std::lock_guard<std::mutex> lg(lock)
. But is there a better way that will be more efficient and faster?