Results 1 to 5 of 5

Thread: What is an Atomic Statement and how to use it?

  1. #1
    Join Date
    Feb 2011
    Posts
    1

    What is an Atomic Statement and how to use it?

    I am trying to write some logic for program that are related to the processors. But while writing the code, I observed that atomic statements are used in it. Let me tell you that I am not aware about this topic. So thought to take some help from you scholars. Please tell me what is an Atomic Statement and how to use it? I have tried to Google the same thing, found some similar topics but there was no solutions appropriate for it. So, I thought to post it on forum. Please provide some information that can be useful for me in this situation.

  2. #2
    Join Date
    Jan 2006
    Posts
    211

    Re: What is an Atomic Statement and how to use it?

    A statement is considered "atomic" when it is running as an instruction. Atomic instructions can be through the Task Scheduler is not interrupted or displaced. In C # example, a simple read or write operation on a 32-bit field, based on a 32-bit CPU is running, type "atomic" because it is processed in a command. The following sample of coding explains the same :
    Code:
     
    class {Atomicity 
    static int x, y; 
    static long z; 
    static void Test () 
    { 
    myLocal long; 
    x = 3 / / Atomic 
    z = 3 / / non-atomic (z is 64 bit) 
    myLocal = z / / Not atomic (z is 64 bit) 
    y + = x / / Not atomic (read / write) 
    x + +; / / non-atomic (read / write) 
    } 
    }
    Read and write operations with greater data capacity (eg 64 bit), however, are on the 32-bit CPU is not atomic, since it is composed of several individuals and executed one after the operations. Here, two separate instructions that read the 32-bit to run. Between these two separate steps, however, it can be used to task- switch , the displacement of a thread that is coming. Now if Thread_A wants to read a 64-bit data, while Thread_B just changed this date Thread_A receives an incorrect value, or a mixture of old and new content.

  3. #3
    Join Date
    Dec 2008
    Posts
    161

    Re: What is an Atomic Statement and how to use it?

    The need for synchronization exists in the case of simple assignments or incrementing a variable. Although this can always be coordinated by the above-mentioned lock techniques and methods, but this is the thread is blocked and must be re-allocated. This leads to delays and administrative overhead. To work around that, the .Net Framework in addition to the lock-in techniques and mechanisms for synchronization that do not block the execution. These prevent blocking, pause or wait states for the threads and are primarily intended for simple operations. This non-blocking synchronization (non-blocking syncs) but are only possible with the use of non-interruptible (atomic) instructions. However, this is not possible without the support of the compiler and the use of "volatile" read and write semantics.

  4. #4
    Join Date
    Mar 2008
    Posts
    192

    Re: What is an Atomic Statement and how to use it?

    Unary operators such as x + + require a first step in reading the variable (x), then it increases and in the third step written back. For example:
    Code:
    class ThreadUnsafe { 
    static int x = 1000; 
    static void Go () { 
    for (int i = 0; i <100; i + +) 
    x -; 
    } 
    }
    In the example above, x is first initialized to 1000 and decrements in the for loop 100 times. Now when this code is 10 threads, one could assume that then the value of x = 0 (1000 - 10 * 100). However, this is not safe! The reason for this lies again in the buffer the data values of x.

  5. #5
    Join Date
    Feb 2009
    Posts
    105

    Re: What is the class Interlocked

    To work around this situation, non-atomic operations can be secured by a lock statement. Locking is almost like a frame around these operations. Much faster and easier but it is through the use of Class Interlocked. The use of this class is not only more efficient than a lock, it also prevents blocking or displacing the thread. And finally, does Interlocked even across process boundaries, while the lock statement is only applicable to threads in the current process.
    Code:
    class Program { 
    static long sum; 
    static void Main () { 
    // Simple increment / decrement operations: 
    Interlocked.Increment (ref sum); // 1 
    Interlocked.Decrement (ref sum); // 0 
    // Add / subtract a value: 
    Interlocked.Add (ref sum, 3); // 3 
    // Read a 64-bit field: 
    Console.WriteLine (Interlocked.Read (ref sum)) // 3 
    // Write a 64-bit field while reading previous value: 
    // (This prints "3" while updating sum to 10) 
    Console.WriteLine (Interlocked.Exchange ref sum (, 10)) // 10 
    // Update a field value only if it matches (10): 
    Interlocked.CompareExchange (ref sum, 123, 10), // 123 
    } 
    }

Similar Threads

  1. Atomic Hybrid Composite Case IP4 for iPhone
    By Camdra in forum Portable Devices
    Replies: 6
    Last Post: 25-06-2011, 10:40 PM
  2. What are an Atomic Variables in Java?
    By Dilbert in forum Software Development
    Replies: 5
    Last Post: 04-01-2011, 07:51 PM
  3. Atomic web browser not working on iPad
    By The EClipse in forum Portable Devices
    Replies: 5
    Last Post: 03-01-2011, 05:05 PM
  4. Replies: 4
    Last Post: 25-02-2009, 08:52 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,707,863.84649 seconds with 17 queries