Is the C language value assignment statement an atomic operation?

Source: Internet
Author: User

I often see some people discuss whether the C language assignment statement is an atomic operation? Is the C ++ statement an atomic operation?

Webopedia:
Atomic implies indivisibility and irreducibility, so an atomic operation must be specified med entirely or not completed MED at all.
An operation during which a processor can simultaneously read a location and write it in the same bus operation. this prevents any other processor or I/O device from writing or reading memory until the operation is complete.

Osdev:
An atomic operation is an operation that will always be executed without any other process being able to read or change state that is read or changed during the operation. it is required tively executed as a single step, and is an important quality in a number of algorithms that deal with multiple indepent processes, both in synchronization and algorithms that update shared data without requiring synchronization.

Related Concepts:
Clock cycle, bus cycle, and command cycle
1. clock cycle: the minimum time unit for the microprocessor to execute commands, also known as the T state. It is usually related to the frequency of the microcomputer.
2. Bus Cycle: the time required by the CPU to perform a read/write operation on the memory or I/O port. For example, the basic Bus Cycle of 8086 microprocessor is from four clock cycles T1 ~ The basic bus cycle of the 80486 microprocessor is composed of two clock cycles: T1 and T2. When the peripheral speed is slow, you can insert the wait period Tw.
3. instruction cycle: the time required by the CPU to execute a command. A command cycle consists of several bus cycles. The execution time of different commands varies. Commands of the same function take different time in different addressing modes.

Bus operation cycle: information exchange between various components of the microcomputer system is completed through the bus operation cycle. A bus cycle is generally divided into the following four stages.
1. Bus request and arbitration phase: when multiple modules initiate bus requests, the arbitration institution must conduct arbitration to determine which module to assign the right to use the bus.
2. Addressing stage: the address and related commands of the memory or I/O port to be accessed by the module that obtains the right to use the bus.
3. Data Transmission Phase: data transmission is performed between the main module (the module that obtains control of the Bus) and other modules.
4. End stage: the main Module removes the relevant information from the bus, and the main module gives control of the bus.

The smallest Execution Unit of the CPU is the command. A command cycle may include multiple bus cycles.
We can get:
1. In a single processor, only one cpu command is included in an operation, which can be an atomic operation. If an operation contains multiple cpu commands, it is not an atomic operation.
2. In a multi-processor, because a cpu instruction cycle may contain multiple bus cycles, Other Processors may access the related status during the execution of one command. Therefore, in multi-processor mode, the bus must be locked during command execution to ensure the atomicity of CPU commands.

Let's take a look at the C language assignment and ++ operations
Code main. c:

[Cpp]
# Include <stdio. h>
 
Void fun1 ()
{
Volatile int m;
Volatile int n;
M = 99;
N = m;
}
 
Void fun2 ()
{
Volatile int n = 10;
N ++;
}
 
Int main (int argc, char ** argv)
{
Fun ();
 
Return 0;
}
Assembly:
Gcc-S main. c
View the commands related to fun1:
[Plain]
Pushq % rbp
Movq % rsp, % rbp
Movl $99,-4 (% rbp)
Movl-4 (% rbp), % eax
Movl % eax,-8 (% rbp)
Leave
Fun2 commands:
[Plain]
Pushq % rbp
Movq % rsp, % rbp
Movl $10,-4 (% rbp)
Leaq-4 (% rbp), % rax
Incl (% rax)
Movl % eax,-4 (% rbp)
Leave

We can see that n = m is two commands:
Movl-4 (% rbp), % eax
Movl % eax,-8 (% rbp)

N ++ three commands:
Leaq-4 (% rbp), % rax
Incl (% rax)
Movl % eax,-4 (% rbp)
There are multiple commands, so they are not atomic operations.

Summary:
Atomic operations are closely related to hardware implementation and compiler implementation. Therefore, it is of little significance to simply discuss atomic operations at the level of advanced languages.


From the night wind Column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.