Thread: atomic variable in C

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    58

    Cool atomic variable in C

    Hi,
    Can you please explane to me the atomic variable.
    1 - What is use for ?
    2 - When should I use it ?

    thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Atomic spinlock
    By Memloop in forum C Programming
    Replies: 6
    Last Post: 03-20-2010, 04:53 PM
  2. Atomic operations
    By MarkZWEERS in forum C++ Programming
    Replies: 3
    Last Post: 02-07-2009, 05:21 PM
  3. is SetSystemTime atomic?
    By bling in forum Windows Programming
    Replies: 1
    Last Post: 09-24-2008, 01:02 PM
  4. Atomic Operations
    By Elysia in forum Windows Programming
    Replies: 27
    Last Post: 03-27-2008, 02:38 AM
  5. Atomic Types
    By miclus in forum C++ Programming
    Replies: 4
    Last Post: 01-01-2004, 08:39 PM