Last active
December 12, 2015 00:58
-
-
Save psychomantys/4687463 to your computer and use it in GitHub Desktop.
This file contains 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 <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