为什么不POSIX的mmap返回挥发性的void *? [英] Why doesn't POSIX mmap return a volatile void*?

查看:168
本文介绍了为什么不POSIX的mmap返回挥发性的void *?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MMAP返回一个void *,但不是挥发性无效* 。如果我使用mmap来映射共享内存,那么另外一个进程可以写入到内存,这意味着随后的两个来自同一个内存位置读取会产生不同的价值观 - 挥发性是为确切的情况。那么,为什么不将其返回挥发性的void *?

Mmap returns a void*, but not a volatile void*. If I'm using mmap to map shared memory, then another process could be writing to that memory, which means two subsequent reads from the same memory location can yield different values -- the exact situation volatile is meant for. So why doesn't it return a volatile void*?

我最好的猜测是,如果你有一个过程,是专门写入共享内存段,它并不需要看通过挥发指针共享内存,因为它总是有一个什么样的present正确理解;任何优化的编译器,以prevent冗余读取不会重要,因为没有什么别的写作和在脚下更改值。还是有其他一些历史的原因?我倾向于说回归挥发性无效* 将是一个更安全的默认,而那些想这个优化则可以手动转换为void *。

My best guess is that if you have a process that's exclusively writing to the shared memory segment, it doesn't need to look at the shared memory through volatile pointers because it will always have the right understanding of what's present; any optimizations the compiler does to prevent redundant reads won't matter since there is nothing else writing and changing the values under its feet. Or is there some other historical reason? I'm inclined to say returning volatile void* would be a safer default, and those wanting this optimization could then manually cast to void*.

POSIX MMAP说明:<一href=\"http://opengroup.org/onlinepubs/007908775/xsh/mmap.html\">http://opengroup.org/onlinepubs/007908775/xsh/mmap.html

POSIX mmap description: http://opengroup.org/onlinepubs/007908775/xsh/mmap.html

推荐答案

通过许多软件系统上运行的根深蒂固的假设是,大多数程序员程序员顺序。这只是最近才开始改变。

The deeply-held assumption running through many software systems is that most programmers are sequential programmers. This has only recently started to change.

MMAP 有几十个不相关的共享内存用途。在一个程序员正在编写多线程程序的情况下,他们必须拿自己的步骤,以确保安全。保护每个变​​量用互斥锁不是默认。同样, MMAP 假设另一个线程将有争议的访问相同的共享内存段,甚至认为段因此映射会被另一个线程访问。

mmap has dozens of uses not related to shared memory. In the event that a programmer is writing a multithreaded program, they must take their own steps to ensure safety. Protecting each variable with a mutex is not the default. Likewise, mmap does not assume that another thread will make contentious accesses to the same shared-memory segment, or even that a segment so mapped will be accessible by another thread.

我也是不相信标记 MMAP 的回报,因为挥发性将对此产生影响。程序员仍然必须确保在访问映射区域的安全,不是吗?

I'm also unconvinced that marking the return of mmap as volatile will have an effect on this. A programmer would still have to ensure safety in access to the mapped region, no?

这篇关于为什么不POSIX的mmap返回挥发性的void *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆