If I have a multi-threaded program that reads a cache-type memory by reference. Can I change this pointer by the main thread without risking any of the other threads reading unexpected values.
As I see it, if the change is atomic the other threads will either read the older value or the newer value; never random memory (or null pointers), right?
I am aware that I should probably use synchronisation methods anyway, but I'm still curious.
Are pointer changes atomic?
Update: My platform is 64-bit Linux (2.6.29), although I'd like a cross-platform answer as well :)
The main risk is that only ptr is atomic. But this does not apply to the values pointed to. To be noted that especially pointers bring further synchronisation risk when you reassign the atomic pointer to a non atomic pointer.
Depending on the compiler and the platform, the pointer assignment may or may not be atomic. C++11 does define a threading behavior and the associated memory model.
A void pointer can hold addresses of any type and can be typecast to any type. It is also called a generic pointer and does not have any standard data type.
void pointer in C / C++A void pointer can hold address of any type and can be typecasted to any type.
As others have mentioned, there is nothing in the C language that guarantees this, and it is dependent on your platform.
On most contemporary desktop platforms, the read/write to a word-sized, aligned location will be atomic. But that really doesn't solve your problem, due to processor and compiler re-ordering of reads and writes.
For example, the following code is broken:
Thread A:
DoWork(); workDone = 1;
Thread B:
while(workDone != 0); ReceiveResultsOfWork();
Although the write to workDone
is atomic, on many systems there is no guarantee by the processor that the write to workDone
will be visible to other processors before writes done via DoWork()
are visible. The compiler may also be free to re-order the write to workDone
to before the call to DoWork()
. In both cases, ReceiveResultsOfWork()
might start working on incomplete data.
Depending on your platform, you may need to insert memory fences and so on to ensure proper ordering. This can be very tricky to get right.
Or just use locks. Much simpler, much easier to verify as correct, and in most cases more than performant enough.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With