Consider the following code. I want to use mutex_by_name()
to create and retrieve mutexes. The lock is not a real lock, but should do its job with a one second gap.
Expected output is that m4.lock()
fails aka prints lock FAILED
because _locked
is already set to true. But it does lock. I'm new to C++ and pretty sure I'm missing something obvious. Can you please explain how to implement that correctly.
#include <iostream>
#include <string>
#include <unordered_map>
#include <unistd.h>
class Mutex {
private:
int _id;
bool _locked = false;
void status(std::string s) {
std::cout << _id << " " << name << " " << s << " " << std::endl;
}
public:
const std::string name;
Mutex(std::string name): name(name) {
static int id = 0;
_id = id++;
status("created");
}
Mutex(const Mutex& m): _id(m._id), _locked(m._locked), name(m.name) {
status("copy-constructed");
}
Mutex(Mutex&& m) = delete;
void operator=(Mutex&) = delete;
~Mutex() {
status("deleted");
}
void lock() {
// YES, THIS IS NOT A REAL AND SAFE LOCK
if (!_locked) {
_locked = true;
status("locked");
} else {
status("lock FAILED");
}
}
};
std::unordered_map<std::string, Mutex> mutexe;
Mutex& mutex_by_name(std::string name) {
mutexe.emplace(name, Mutex(name));
auto found = mutexe.find(name);
return found->second;
}
using namespace std;
int main() {
cout << "# 1" << endl;
Mutex m1 = mutex_by_name("hello");
m1.lock();
sleep(1);
cout << "# 2" << endl;
Mutex m4 = mutex_by_name("hello");
m4.lock();
sleep(1);
}
Aucun commentaire:
Enregistrer un commentaire