Create guard for flush

This commit is contained in:
2025-05-26 14:04:15 +02:00
parent e4c8ec9621
commit 6e9ac56a3c
4 changed files with 84 additions and 44 deletions

50
src/guarder.h Normal file
View File

@@ -0,0 +1,50 @@
#ifndef _GUARDER_H
#define _GUARDER_H
#include <sys/types.h>
#include <sys/shm.h>
#include <sys/ipc.h>
class guarder{
private:
const int shmid;
void setFlag(bool value)
{
bool *flag = static_cast<bool *>(shmat(shmid, nullptr, 0));
*flag = value;
shmdt(flag);
}
public:
guarder() : shmid(shmget(IPC_PRIVATE, sizeof(bool), 0666 | IPC_CREAT))
{
setFlagOff();
}
void setFlagOff()
{
setFlag(false);
}
void setFlagOn()
{
setFlag(true);
}
bool isFlagOn()
{
bool *flag = static_cast<bool *>(shmat(shmid, nullptr, 0));
bool ret = *flag;
shmdt(flag);
return ret;
}
~guarder()
{
shmctl(shmid, IPC_RMID, nullptr);
}
};
#endif // _GUARDER_H