Last active
April 10, 2016 14:24
-
-
Save Hikari9/dd2dc96f760a31018179adfe8dccd270 to your computer and use it in GitHub Desktop.
Semaphore Class and Shared Memory Class
This file contains hidden or 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
| #ifndef INCLUDE_SEMAPHORE | |
| #define INCLUDE_SEMAPHORE 1 | |
| #include <sys/types.h> | |
| #include <sys/ipc.h> | |
| #include <sys/sem.h> | |
| #include <sys/shm.h> | |
| #include <unistd.h> | |
| class semaphore { | |
| private: | |
| int _id, _key; | |
| static sembuf WAIT[2], SIGNAL[1]; | |
| public: | |
| semaphore(): _id(-1) {} | |
| semaphore(int key) {this->key(key);} | |
| void key(int new_key) { | |
| // set semaphore's key | |
| _key = new_key; | |
| _id = semget(_key, 1, IPC_CREAT | 0666); | |
| } | |
| inline int id() const {return _id;} | |
| inline int key() const {return _key;} | |
| inline int wait() const {return id() == -1 ? -1 : semop(id(), (sembuf*) WAIT, 2);} | |
| inline int signal() const {return id() == -1 ? -1 : semop(id(), (sembuf*) SIGNAL, 1);} | |
| }; | |
| sembuf semaphore::WAIT[2] = {{0, 0, SEM_UNDO}, {0, 1, SEM_UNDO | IPC_NOWAIT}}; | |
| sembuf semaphore::SIGNAL[1] = {{0, -1, SEM_UNDO | IPC_NOWAIT}}; | |
| #endif /* __INCLUDE_SEMAPHORE__ */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment