boost::interprocess 中 win32 文件锁定的模拟是什么?

发布于 2024-07-13 00:39:57 字数 59 浏览 4 评论 0原文

我应该使用什么同步机制来提供对 boost 中文本文件的独占访问? 该文件可能仅由一个进程的线程访问。

What sync mechanism should I use to give exclusive access to the text file in boost?
The file will likely be accessed by threads from only one process.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暗地喜欢 2024-07-20 00:39:57

文件锁定 API 通常用于进程间锁定。 如果您在单个进程中,则 Boost.Thread 包 中的所有内容适合您需求的就可以了。 外部进程应该使用Boost.Interprocess。 您可能想阅读来自 Boost.Interprocess 的以下警告:

Caution: Synchronization limitations

如果您计划像命名互斥体一样使用文件锁,请小心,因为可移植文件锁具有同步限制,主要是因为不同的实现(POSIX、Windows)提供不同的保证。 进程间文件锁具有以下限制:

  • 未指定 file_lock 是否同步同一进程中的两个线程。
  • 未指定进程是否可以使用两个指向同一文件的 file_lock 对象。

第一个限制主要来自 POSIX,因为文件句柄是每进程属性而不是每线程属性。 这意味着如果一个线程使用 file_lock 对象锁定文件,其他线程将看到该文件已锁定。 另一方面,Windows 文件锁定机制提供线程同步保证,因此尝试锁定已锁定文件的线程将被阻塞。

第二个限制来自于文件锁定同步状态与 Windows 中的单个文件描述符相关联的事实。 这意味着如果创建两个指向同一文件的 file_lock 对象,则无法保证同步。 在 POSIX 中,当使用两个文件描述符来锁定文件时,如果关闭一个描述符,则调用进程设置的所有文件锁都会被清除。

总而言之,如果您计划在进程中使用文件锁定,请使用以下限制:

  • 对于每个文件,每个进程使用一个 file_lock 对象。
  • 使用同一线程锁定和解锁文件。
  • 如果您使用 std::fstream/native 文件句柄写入该文件,同时对该文件使用文件锁,则在释放该文件的所有锁之前不要关闭该文件。

The file locking APIs are generally for inter process locking. If you are in a single process everything in Boost.Thread package that suits your needs will do. Outside processes the Boost.Interprocess should be used. You might want to read the following warning from 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. Interprocess file locks have the following limitations:

  • It's unspecified if a file_lock synchronizes two threads from the same process.
  • It's unspecified if a process can use two file_lock objects pointing to the same file.

The first limitation comes mainly from POSIX, since a file handle is a per-process attribute and not a per-thread attribute. This means that if a thread uses a file_lock object to lock a file, other threads will see the file as locked. Windows file locking mechanism, on the other hand, offer thread-synchronization guarantees so a thread trying to lock the already locked file, would block.

The second limitation comes from the fact that file locking synchronization state is tied with a single file descriptor in Windows. This means that if two file_lock objects are created pointing to the same file, no synchronization is guaranteed. 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.

To sum up, if you plan to use file locking in your processes, use the following restrictions:

  • For each file, use a single file_lock object per process.
  • Use the same thread to lock and unlock a file.
  • If you are using a std::fstream/native file handle to write to the file while using file locks on that file, don't close the file before releasing all the locks of the file.
魔法少女 2024-07-20 00:39:57

我想它是 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);
}

它是一致的使用锁的非Boost实现

    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 

I suppose it is 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 
汹涌人海 2024-07-20 00:39:57

如果您确定只能从一个进程访问它,则在线程本地存储中使用带有文件句柄的读写锁可能是一种解决方案。 这将模拟上述情况,只有一名作者和多名读者。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文