top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

regarding __atomic_load_n()

+1 vote
1,330 views

When I do something like:

thread 1:
 int count = 0;
 int *thread_var = 

thread 2:    
 __atomic_store_n(thread_var, 7, __ATOMIC_RELEASE);

Do I then need to do the following in thread 3?

thread 3 option 1:
 *thread_var = __atomic_load_n(thread_var, __ATOMIC_ACQUIRE);

Or do this suffice?

thread 3 option 2:
 __atomic_load_n(thread_var, __ATOMIC_ACQUIRE);
posted Sep 18, 2013 by Anderson

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

1) Do I then need to do the following in thread 3?

What are you trying to do?

2) *thread_var = __atomic_load_n(thread_var, __ATOMIC_ACQUIRE);

This reads the value of *thread_var, returns it, then (non-atomically) stores it back in *thread_var. What's the point of that? If you need the value, you still haven't read it back from *thread_var (and you may have created a race condition by writing to *thread_var.)

3) __atomic_load_n(thread_var, __ATOMIC_ACQUIRE);

This loads the value then discards it again. I think you want:

int var = __atomic_load_n(thread_var, __ATOMIC_ACQUIRE);
answer Sep 18, 2013 by Amit Parthsarthi
Not really. What I have is a variable which is shared by two threads, but for the most part only read and written in one of them. One thread runs continually in a loop, the other does not. Then, occasionally, I'm pausing the looping thread. From the other thread I then want to read *thread_val and set it to another value. Then I restart the looping thread which should now have the be able to see the new value of *thread_val.

I was merely concerned that the new value of *thread_val was returned from __atomic_load_n() but not yet necessarily accessible by simply dereferencing thread_val.

Anyways, you've cleared up my concerns, thanks.
Similar Questions
+1 vote

I wrote a multi-threaded program using pthread library, but I want execution of threads in a specific order.
What I have to do ?

+2 votes

write functions to read and write in a hash table in a multi threaded environment. Approach should give decent optimization in terms of time complexity.

+1 vote

As far as I know single assignment should be atomic operation so should be OK but not sure.
Any thoughts?

+2 votes

Here my doubt is about acquire mutex lock.

Here pthread_mutex_t lock ; is also a global variable shared to threads. Accessing of this global variable (lock) will it be shame as accessing of other global variables ? If same, then don't we face same problem what we will face for other global variables ? if not ,how this is discriminated from other global variables ?

...