I think I may use a separate file for locking over in /run/lock. So, I can create a class similar to below. The destructor will also close the file if I have it locked so that the file will get unlocked when the class goes out of scope. So, something like:
class MyLockClass
private:
int fd_ = -1;
public:
MyLockClass();
bool lock() {
fd_ = open("/run/lock/myfile.lock", O_RDONLY | O_CREAT);
flock(fd, LOCK_EX) ;
return true;
}
void unlock() {
if (fd_ != -1) {
close(fd_);
fd_ = -1;
}
return;
}
~MyLockClass() {
unlock();
}
As long as all my routines that want to access the main file use this class to get a lock it should work out. I can keep using streams for the main file once it gets the lock on this other file.
Thanks
Chris
class MyLockClass
private:
int fd_ = -1;
public:
MyLockClass();
bool lock() {
fd_ = open("/run/lock/myfile.lock", O_RDONLY | O_CREAT);
flock(fd, LOCK_EX) ;
return true;
}
void unlock() {
if (fd_ != -1) {
close(fd_);
fd_ = -1;
}
return;
}
~MyLockClass() {
unlock();
}
As long as all my routines that want to access the main file use this class to get a lock it should work out. I can keep using streams for the main file once it gets the lock on this other file.
Thanks
Chris
Statistics: Posted by chriskot870 — Tue Dec 31, 2024 6:02 pm