[英]What is an analog for win32 file locking in boost::interprocess?
What sync mechanism should I use to give exclusive access to the text file in boost? 我应该使用哪种同步机制以独占方式访问boost中的文本文件? The file will likely be accessed by threads from only one process. 线程可能仅从一个进程访问该文件。
The file locking APIs are generally for inter process locking. 文件锁定API通常用于进程间锁定。 If you are in a single process everything in Boost.Thread package that suits your needs will do. 如果您在一个进程中,那么Boost.Thread包中满足您需求的一切都将满足。 Outside processes the Boost.Interprocess should be used. 外部进程应使用Boost.Interprocess 。 You might want to read the following warning from Boost.Interprocess: 您可能想从Boost.Interprocess阅读以下警告:
Caution: Synchronization limitations
If you plan to use file locks just like named mutexes, be careful, because portable file locks have synchronization limitations, mainly because different implementations (POSIX, Windows) offer different guarantees. 如果您打算像命名互斥锁一样使用文件锁,请小心,因为可移植文件锁具有同步限制,主要是因为不同的实现(POSIX,Windows)提供了不同的保证。 Interprocess file locks have the following limitations: 进程间文件锁具有以下限制:
file_lock
synchronizes two threads from the same process. 未指定file_lock
同步来自同一进程的两个线程。 file_lock
objects pointing to the same file. file_lock
确定某个进程是否可以使用两个指向同一文件的file_lock
对象。 The first limitation comes mainly from POSIX, since a file handle is a per-process attribute and not a per-thread attribute. 第一个限制主要来自POSIX,因为文件句柄是按进程属性而不是按线程属性。 This means that if a thread uses a file_lock
object to lock a file, other threads will see the file as locked. 这意味着,如果线程使用file_lock
对象锁定文件,则其他线程会将文件视为已锁定。 Windows file locking mechanism, on the other hand, offer thread-synchronization guarantees so a thread trying to lock the already locked file, would block. 另一方面,Windows文件锁定机制提供了线程同步保证,因此尝试锁定已锁定文件的线程将被阻塞。
The second limitation comes from the fact that file locking synchronization state is tied with a single file descriptor in Windows. 第二个限制来自以下事实:在Windows中,文件锁定同步状态与单个文件描述符绑定在一起。 This means that if two file_lock objects are created pointing to the same file, no synchronization is guaranteed. 这意味着,如果创建两个指向同一文件的file_lock对象,则无法保证同步。 In POSIX, when two file descriptors are used to lock a file if a descriptor is closed, all file locks set by the calling process are cleared. 在POSIX中,如果关闭了描述符,则使用两个文件描述符来锁定文件时,将清除调用进程设置的所有文件锁。
To sum up, if you plan to use file locking in your processes, use the following restrictions: 综上所述,如果您打算在进程中使用文件锁定,请使用以下限制:
file_lock
object per process. 对于每个文件,每个进程使用一个file_lock
对象。 I suppose it is acquire_file_lock 我想这是acquire_file_lock
inline bool acquire_file_lock(file_handle_t hnd)
{
struct ::flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
return -1 != ::fcntl(hnd, F_SETLKW, &lock);
}
It is consistent with a non-boost implementation of a lock . 它与锁的非增强实现一致。
struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0 };
int fd;
fl.l_pid = getpid();
if (argc > 1)
fl.l_type = F_RDLCK;
if ((fd = open("lockdemo.c", O_RDWR)) == -1) {
perror("open");
exit(1);
}
printf("Press <RETURN> to try to get lock: ");
getchar();
printf("Trying to get lock...");
if (fcntl(fd, F_SETLKW, &fl) == -1) {
perror("fcntl");
exit(1);
}
printf("got lock\n");
printf("Press <RETURN> to
If you are sure it will only be accessed from one process, a read-write lock with file handles in thread local storage could be a solution. 如果确定只能从一个进程访问它,则在线程本地存储中具有文件句柄的读写锁可能是一种解决方案。 That would simulate the above with only one writer but several readers. 那将只用一个作者,但要有几个读者来模拟以上情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.