Skip to content

Instantly share code, notes, and snippets.

@psychomantys
Last active December 12, 2015 00:58
Show Gist options
  • Save psychomantys/4687463 to your computer and use it in GitHub Desktop.
Save psychomantys/4687463 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
class Mutex{
public:
typedef volatile unsigned int id_t;
private:
id_t id_global;
id_t base;
inline void block( const id_t &id ){
while( id!=base );
}
public:
Mutex() : id_global(0), base(1)
{ }
void lock(){
const id_t id=++id_global;
block(id);
}
void release(){
++base;
}
};
class Critical{
private:
Mutex &mutex;
public:
Critical( Mutex &mutex ) : mutex(mutex) {
mutex.lock();
}
~Critical(){
mutex.release();
}
};
int main(){
Mutex x;
{
Critical rc(x);
cout << "Printf!!!!\n" ;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment