0

I have done some research without success about the way to access a pointed variable directly through RAM, being sure the compiler will not access to a registered copy.

int32_t* a;
void main()
{
    a = (int32_t*)malloc(4);
}

void mainBis()
{
    ???
}

How to be sure accessing to *a the same way as if it was volatile?

Thanks

P.S.: Even if it's called main and mainBin here, it's just two different threads.

Archivist
  • 1
  • 1
  • `main` should return an `int`. Read: [What should main() return in C and C++?](http://stackoverflow.com/q/204476) – AliciaBytes Apr 09 '14 at 11:57
  • Why do you want this? What are you doing with `a` that causes you to believe it requires special handling? – Casey Apr 09 '14 at 12:51
  • Please, don't just read the code, you can't say TLDR... Consider it's no a main but a thread, I could have named it `the_great_ping_pong_story()` nothing had changed... – Archivist Apr 09 '14 at 15:27

3 Answers3

0

Simply declare a as int32_t volatile * a. That way the compiler can not assume that two expressions using *a would be equal, even if there are no writes to memory of changes to a in between.

Lindydancer
  • 25,428
  • 4
  • 49
  • 68
  • Will it not just mark the pointed variable as `volatile`? I'll give a try. Thanks – Archivist Apr 09 '14 at 15:24
  • When the compiler access the variable via the pointer, it has no knowledge about which variable the pointer actually points to, or even if it is a variable. – Lindydancer Apr 09 '14 at 19:06
0

What you are trying to do using double =, == stands for equality check and not for assignment.

a == (int32_t*)malloc(4);

should use single = as in:

a = (int32_t*)malloc(4);
Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
0

This seems like an XY problem.

It's a bit unclear what you're asking - my understanding is you want to access a from different threads (maybe on different cores or even processors), so you're wanting to make sure it gets written out to RAM, rather than just cached or reused from a register. Is that right?

Concurrency is kind of a big topic and we don't know the details of the system you're running - when the two functions are called in relation to each other.

First things first - what you're starting to get at is the volatile keyword. Declaring the variable as volatile int32_t* a; tells the compiler it might be changed at any time from another thread, but doesn't guarantee anything about its status in memory vs the cache. If the 2 threads are known to be running on the same core and you're sure you'll be reading from a after you've set it in main().

If you're not sure about which order the read & write will execute, you'll need to do your own thread synchronisation, which works differently depending on which architecture & libraries you're using. In general, you'd want something like this psuedocode:

int32_t* a;
semaphore_t aIsValid;

void main()
{
    a = (int32_t*)malloc(4);
    signal_semaphore(aIsValid);
}

void mainBis()
{
    wait_for_semaphore(aIsValid);
    // read from a
}

On most modern systems, using synchronisation primitives like semaphores also ensures cache coherence between threads, so a will have the same value everywhere after aIsValid releases.

Community
  • 1
  • 1
etheranger
  • 1,233
  • 7
  • 17
  • Thanks, you confirmed what was unclear to me, therefore you detailed semaphores. The thing that was strange was the pointer and its target to be considered as volatile if only the pointer was. Seems it is. I thought that the pointer will be volatile (concurrency friendly) but I was not sure the target will be. – Archivist Apr 14 '14 at 14:12