Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #67786 > unrolled thread
| Started by | Vir Campestris <vir.campestris@invalid.invalid> |
|---|---|
| First post | 2019-08-19 21:56 +0100 |
| Last post | 2019-08-20 16:18 +0200 |
| Articles | 1 on this page of 21 — 11 participants |
Back to article view | Back to comp.lang.c++
"volatile" on variables and current processors Vir Campestris <vir.campestris@invalid.invalid> - 2019-08-19 21:56 +0100
Re: "volatile" on variables and current processors red floyd <dont.bother@its.invalid> - 2019-08-19 15:20 -0700
Re: "volatile" on variables and current processors red floyd <dont.bother@its.invalid> - 2019-08-19 15:21 -0700
Re: "volatile" on variables and current processors "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2019-08-19 16:37 -0700
Re: "volatile" on variables and current processors Vir Campestris <vir.campestris@invalid.invalid> - 2019-08-20 21:58 +0100
Re: "volatile" on variables and current processors scott@slp53.sl.home (Scott Lurndal) - 2019-08-20 22:18 +0000
Re: "volatile" on variables and current processors Vir Campestris <vir.campestris@invalid.invalid> - 2019-08-22 21:39 +0100
Re: "volatile" on variables and current processors Paavo Helde <myfirstname@osa.pri.ee> - 2019-08-21 09:02 +0300
Re: "volatile" on variables and current processors "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2019-08-20 23:10 -0700
Re: "volatile" on variables and current processors David Brown <david.brown@hesbynett.no> - 2019-08-21 08:34 +0200
Re: "volatile" on variables and current processors Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2019-08-21 20:39 +0100
Re: "volatile" on variables and current processors Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2019-08-21 21:02 +0100
Re: "volatile" on variables and current processors David Brown <david.brown@hesbynett.no> - 2019-08-22 13:30 +0200
Re: "volatile" on variables and current processors Bonita Montero <Bonita.Montero@gmail.com> - 2019-08-21 08:43 +0200
Re: "volatile" on variables and current processors "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2019-08-20 02:22 +0200
Re: "volatile" on variables and current processors David Brown <david.brown@hesbynett.no> - 2019-08-20 10:05 +0200
Re: "volatile" on variables and current processors Keith Thompson <kst-u@mib.org> - 2019-08-19 17:08 -0700
Re: "volatile" on variables and current processors Bonita Montero <Bonita.Montero@gmail.com> - 2019-08-20 06:08 +0200
Re: "volatile" on variables and current processors David Brown <david.brown@hesbynett.no> - 2019-08-20 10:14 +0200
Re: "volatile" on variables and current processors Juha Nieminen <nospam@thanks.invalid> - 2019-08-20 13:03 +0000
Re: "volatile" on variables and current processors David Brown <david.brown@hesbynett.no> - 2019-08-20 16:18 +0200
Page 2 of 2 — ← Prev page 1 [2]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2019-08-20 16:18 +0200 |
| Message-ID | <qjgvfi$ikl$1@dont-email.me> |
| In reply to | #67813 |
On 20/08/2019 15:03, Juha Nieminen wrote:
> Vir Campestris <vir.campestris@invalid.invalid> wrote:
>> And it's got lots of use of
>> volatile in the old way - don't cache this please, another thread may
>> touch it.
>
> volatile does in no way make a variable thread-safe. And it has never been
> even really meant for that.
>
> The main use for volatile is in certain architectures (eg. certain
> embedded systems) where eg. certain memory locations have more significance
> to the system than being merely dummy storage for a value. For example,
> a particular memory location might be mapped by the hardware to be input
> and output to a port (which may be, for instance, some kind of 7-segment
> LCD display or the like). Thus writing to that memory location will output
> the value to that port, and readint that location will read the port. In
> these architectures telling the compilar that eg. a pointer to that memory
> location is pointing to a volatile value may be important, as it (usually)
> prevents the compiler from optimizing away reads and writes to it.
>
That was the original motivation for "volatile", and it is still a major
use-case. (Usually the hardware will have features for treating the
memory locations of these hardware registers in special ways, such as
skipping caches and disallowing re-ordering in buffers or speculative
accesses - this gives you some of the effects of C++11 atomic accesses
on these addresses even when you just have a simple "volatile".)
"volatile" also finds use in single core systems - it has long been
essential in microcontroller programming for handling data between
interrupt functions and other code, and on most microcontrollers it is
still sufficient (while C++11 atomics would be overkill and slower).
Sometimes it is also used to force particular effects. For example, if
you have a pure calculation function "foo" (i.e., it has no side effects
and does not access non-const global data), and you want to know how
long it takes to run, you might try:
const int x = 123;
auto start_time = get_time();
for (int i = 0; i < 1000000; i++) {
int y = foo(x);
}
auto end_time = get_time();
std::cout << "Time for 1000000 runs is " <<
(end_time - start_time) << "\n";
Enable optimisation, and the compiler may happily remove the entire loop.
But if you use:
volatile const int x = 123;
and
volatile int y = foo(x);
you get the timing you want.
> In general, for "normal" variables or pointers you don't want to
> needlessly declare then volatile, because they can become really inefficient,
> as the compiler usually obey the instruction and literally read and write
> to that variable in memory every single time it's done in the code.
> (For example, declaring a loop counter variable volatile for no good
> reason would be rather silly.)
>
Yes.
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.c++
csiph-web