Hi,
Can you please explane to me the atomic variable.
1 - What is use for ?
2 - When should I use it ?
thanks
Hi,
Can you please explane to me the atomic variable.
1 - What is use for ?
2 - When should I use it ?
thanks
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Generally it's the operations on a variable that are "atomic", as opposed to the variable itself being atomic. This is a method used to safely update and/or fetch data shared between tasks, threads, and interrupt routines, and between processors on multi-processor motherboards (most server oriented motherboards include support for this). In order for a variable to be "atomic", the compiler will ensure that some set of operations on that variable will be atomic, but generally those operations are limited to a single operator or function call. For example, assuming "atomic int A", then the compiler could make A += 1; an atomic operation, but it couldn't do much with two lines of code such as "B = A; A = C;", you'd need an atomic function call for this. The operations on memory shared between processors is usually limited to just a few or perhaps a single instruction type, depending on the processor. On an Intel X86 processor, there's a "locK' bit used to lockout other processors during a memory operation (this also causes the other processors to invalidate any cached copies of the affected part of memory), and it's always used on an XCHG (exchange) instruction. There's a "lock" instruction that can be use to "locK' some memory based instructions, such as BTC, BTR, BTS (bit test and complement, reset, set), ADD, OR, ADC, SBB, AND, SUB, XOR, NOT, NEG, INC, DEC.
Syncrhonization between tasks and threads on is often done using mutex (mutually exclusive) and/or semaphore (a count that is considered set if non-zero).
Last edited by rcgldr; 04-17-2013 at 03:09 PM.