compare-and-swap

Ring buffer with atomic indexes

半世苍凉 提交于 2021-02-19 08:45:38
问题 I have struggled with what must be a fundamental misunderstanding of how atomics work in C++. I have written the code below to implement a fast ring buffer using atomic variables for indexes so multiple threads can write to and read from the buffer. I've whittled the code down to this simple case (which I realize is still a little long. Sorry.). If I run this on either Linux or Mac OS X, it will work some of the time, but it will also throw exceptions at least 10% of the time. It also seems

C++ atomic CAS(compare-and-swap) operation does not change value

*爱你&永不变心* 提交于 2021-02-17 03:44:34
问题 In the following example, what actually happens? Why value does not changes after successful exchange? Live: https://wandbox.org/permlink/f5VYSKfQ9UJqa8FQ std::atomic<bool> flag{false}; int main() { std::thread thread([](){ while(true){ // wait until flag not becomes true { bool expect = true; while(!flag.compare_exchange_strong(expect, false, std::memory_order_acq_rel)){ std::cout << "wait" << std::endl; } } std::cout << "work" << std::endl; } }); flag.store(true, std::memory_order_release);

compareAndSet on processors that does not support CAS operation

早过忘川 提交于 2021-02-10 03:31:00
问题 Today I was asked the next question on an interview: "What is going on with compareAndSet method from AtomicLong in case you call it on a machine with a processor that does not support CAS operation". Could you please help me with this question and provide some links to a comprehensive description if possible? 回答1: From Java Concurrency in Practice 15.2.3 CAS support in the JVM : On platforms supporting CAS, the runtime inlines them into the appropriate machine instruction(s); in the worst

compareAndSet on processors that does not support CAS operation

别说谁变了你拦得住时间么 提交于 2021-02-10 03:28:57
问题 Today I was asked the next question on an interview: "What is going on with compareAndSet method from AtomicLong in case you call it on a machine with a processor that does not support CAS operation". Could you please help me with this question and provide some links to a comprehensive description if possible? 回答1: From Java Concurrency in Practice 15.2.3 CAS support in the JVM : On platforms supporting CAS, the runtime inlines them into the appropriate machine instruction(s); in the worst

Real-world examples for ABA in multithreading [closed]

笑着哭i 提交于 2021-02-06 02:40:34
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . I'm looking for some nice real-world examples of the ABA-problem causing trouble in multithreaded code. The ABA-problem occurs in

Real-world examples for ABA in multithreading [closed]

一世执手 提交于 2021-02-06 02:34:57
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . I'm looking for some nice real-world examples of the ABA-problem causing trouble in multithreaded code. The ABA-problem occurs in

Real-world examples for ABA in multithreading [closed]

ぃ、小莉子 提交于 2021-02-06 02:34:19
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . I'm looking for some nice real-world examples of the ABA-problem causing trouble in multithreaded code. The ABA-problem occurs in

Do Atomic variables guarantee a - “happens before relationship”?

夙愿已清 提交于 2021-01-20 12:43:13
问题 I have a requirement where I need to publish results of 'n' threads upon their completion. For checking if all the threads completed, I am using an AtomicInteger (incrementAndGet()) and comparing its value against a final variable. Before doing the check, I'm writing the results of individual threads to a shared object (into a concurrent-hashmap, as non-synchronized data structures dint seem to be adequate). So, my question is will all the threads complete writing to the shared object (and

Does cmpxchg write destination cache line on failure? If not, is it better than xchg for spinlock?

二次信任 提交于 2020-08-08 06:19:28
问题 I assume simple spinlock that does not go to OS waiting for the purposes of this question. I see that simple spinlock is often implemented using lock xchg or lock bts instead of lock cmpxchg . But doesn't cmpxchg avoid writing the value if the expectation does not match? So aren't failed attempts cheaper with cmpxchg ? Or does cmpxchg write data and invalidate cache line of other cores even on failure? This question is similar to What specifically marks an x86 cache line as dirty - any write,

CAS based algorithms - how threads sees latest values?

柔情痞子 提交于 2020-06-01 05:50:48
问题 I recently came to know about CAS - Compare and Set and I tried to know more about it. I checked online in quest to know more and came across this link. How is it possible that without locking , using CAS threads can get / update values. For example, in multicore processors they keep values in their own cache (and that is why we use volatile when we want threads to read latest values). So, even if, there is CAS , won't threads trying to do this operation be reading from their own cache? Are