Read and Write memory location

Hi,

I've got some experience with hardware languages and I know the option exists to do the following:
1
2
if(readWrite(var,true))
 do_something();

Anyway, the output should be equivalent to:
1
2
3
4
if(var)
 do_something();
else
 var = true;


It's like reading the variable but writing a new value to it after the read. This reduces disk latency etc...

I don't seem to find this in c++...
closed account (S6k9GNh0)
I would hope you wouldn't find that in C++ as that would defeat the purpose of an if else statement and is possibly a slower method of accomplishing such a task.
... As the code pointed out, it is faster, as it doesn't need to access the same memory field twice... it reduces seek latency
closed account (S6k9GNh0)
Let's write some hypothetical code:

1
2
3
4
5
6
7
bool readWrite(bob &something, bob if_not)
{
    if (something)
        return false;
    else
        something = if_not;
}


To begin, functions themselves have overhead. By not using such a function, you do not incur the overhead of both passing variables onto a stack and then calling the function.

Using the above function, I've illustrated a possible implementation of such a function. Please try and do better so as to make cycle usage and "seek latency" go down. There *might* be some assembly that could outperform basic syntax but compilers tend to be pretty smart. Who knows?

What hardware languages were you using?
Last edited on
This topic rings only one bell to me: Interlocked functions in the Windows API. There are certain functions that are only available in certain architectures, so I guess they do have something to do with the hardware. I don't know assembly so I just can't be sure.

I also know they provide atomic operations, but I don't know if they are faster at all than a non-atomic counterpart.

If this is Windows and you would like to investigate, an example of such functions is InterlockedIncrement().
We're not on the same page here :)

There is in hardware a command that does the following in an atomic fashion:
T oldValue = readWrite(T* memorylocation, T newValue);

The result of doing it is for example:
1
2
int i = 5;
int value = readWrite(&i, 1);

then i = 1 and value = 5.

The idea is that no matter what, the memorylocation's value is changed to newValue. However, I wanted to know the old value as well. In essense the hardware function reads the old value, stores the new one, and returns the old (all in one disk access). Unfortunately I forgot the name...

I simply want to access this functionality in C.

Maybe swapping is an option?
Last edited on
closed account (S6k9GNh0)
I think I get what your saying... I can think it out in my fugly idea of CQ's assembly but I'm not sure its worth your time. Maybe suggest a new syntax to support such an idea? You still have function overhead so using a function for such a thing probably doesn't pan out well. Using inline assembly in an inline function doesn't really make sense to me either.
Last edited on
Yeah, I was just wondering if there already existed an std function to do this. I wasn't planning on writing an unefficient version myself :)

FYI, I think in hardware it is most commonly refered to as a getAndSet operation. Though that might be informal :s
Topic archived. No new replies allowed.