Recently, in the user State, we suddenly needed to use atomic variables, and didn't want to compile boost on our own. We accidentally found that GCC still has this set of built-in functions.
// First perform the operation, and then return the changed Value Type _ sync_fetch_and_add (type * PTR, type value); Type _ sync_fetch_and_sub (type * PTR, type value ); type _ sync_fetch_and_or (type * PTR, type value); Type _ sync_fetch_and_and (type * PTR, ty
Multithreading is often used in recent work, but the problem of multi-threaded shared variables needs to be solved. Fortunately,. NET provides the interlocked class for us, but Microsoft provides atomic operation classes for variables shared by multiple threads. One of the frequently used methods is interlocked. increment () and interlocked. decrement ().
The following describes the two methods on msdn:
Increment andThe decrement method increments or
When the Linux kernel prints "bug: Scheduling while atomic" and "Bad: scheduling from the idle thread" errors, it usually calls sleep functions in the interrupt processing function, such as semaphore, mutex, sleep, and other sleep functions, while the Linux kernel requires that the system height and preemption are not allowed during Interrupt Processing, and other tasks can be performed only after the interrupt processing is completed. Therefore, it m
:
Add_counter:
PUSHL%EBP
MOVL%esp,%EBP
MOVL counter,%eax
Addl $,%eax
MOVL%eax, counter
POPL%EBP
Ret
look at these three lines in red. What's going on? Why do you put the counter into the register EAX first, and then the EAX to operate, and then eax into counter. In that case, the++counter will never be atomic! Must be protected by a lock! Let's see why the assembly is such a behavior. What kind of service does
InterlockedIncrement and InterlockedDecrementThe atomic addition and subtraction of the implementation number.What is atomic addition and subtraction?For example, if a variable is Long value = 0;First of all, the normal situation of the addition and subtraction operation: value+=1;1: The system takes out the value from the space of value and dynamically generates a space to store the values taken out;2: Add
In objective-C, attributes are usually set with the following keywords:
-- Assign: Specifies the setter method to use a simple value assignment, which is the default operation. You can use this attribute for scalar type (such as INT. You can imagine a float. It is not an object, so it cannot be retain or copy.
-- Retain: specifies that the retain should be called on a later object. The previous value sends a release message. You can imagine an nsstring instance, which is an object and you ma
In android, cmpxchg performs atomic operations, such as auto-addition and auto-subtraction.
Cmpxchg exists in 486 and more advanced CPUs. Other types of CPUs, such as Arm, do not have this command.
Cmpxchg commands are encapsulated in Functionsandroid_atomic_cmpxchgThe function is located in atomic. c and is preprocessed.
#elif defined(__i386__) || defined(__x86_64__)
Protected.1. source code analysis:
This question is seldom met, but the answer is of course not.Atomic added a lock in the set method, preventing multi-threading from writing to the property, causing difficult-to-predict values. But it's also just a read-write lock. and thread safety is actually worse. Look underneath. @interface monperson: nsobject @property (copy) nsstring * firstName; @property (copy) NSString * LastName; -(nsstring *) FullName; @endThread a:p.firstname = @ "Rob "; Thread b:p.firstname = @ "Robert"; Thread
One is to use boost::atomic; a direct lock; The code is simple:#include The test results are as follows:[email protected]:~/code$./test 0 1000loopnum:10000 test_atomic Cost 0 ms[Email protected]:~/code$./test 1 1000Loopnum:10000test_lock Cost 0 ms[Email protected]:~/code$./test 1 100000Loopnum:10000test_lock Cost 9 ms[email protected]:~/code$./test 0 100000loopnum:10000 test_atomic cost 2 msAs the number of loops increases, the gap becomes larger, and
accumulated value is not immediately output, resulting in output confusion, but still can be added to 600. Knowledge output is unsafe.3. Using an atomic type, such as changing the int count type of the above code to Atomicinteger count type, we know that getting the value of count and then adding another 1 is a problem that can occur, that is, the result is a repeating number. The Atomicinteger type solves this problem in a synchronous way. Such as:R
1000), or the operation that failed together (a account returned to its original state and B account back to its original state) was called an atomic operation.If a transaction can be thought of as a program, it is either completely executed or not executed at all. This property is called atomicity.The use of [mutex mutex] in Python prevents multiple threads from simultaneously modifying a variable caused by an error.Mutex = Threading. Lock () Create
Tags: collect atomic concept date basic reading for nbspFirst, in principle, MONGDB has no concept of business.A transaction has an acid concept, such as atomicity, in which a transaction either succeeds or fails altogether.For example, considering a transfer business, a transfer from a 100 to B will be divided into two steps:A = A-100;b = b + 100;In Mongdb, if a = A-100 is executed, it will be put into effect directly, there is no concept of rollback
Java has a lot of thread-safe basic module classes, and in general, these basic module classes can meet all of the operations we need, but more often than not, they do not meet all of our needs. At this point, we need to find a way to add a new atomic operation without destroying the existing thread-safe class. The following 4 scenarios are available:1 Modify the source code of the class to add a new atomic
In the multi-threaded environment to count, you need to ensure that the consistency of the count variables and thread safety to ensure that the count is correct in multi-threaded environment.The atomic count is the feature that provides this requirement, which guarantees that the count operation will never be interrupted by any other practice or event when it is completed, and the following is an atomic cou
The java.util.concurrent.atomic provides classes on atomic operations, including: Atomiclong, Atomicinteger, Atomicboolean. And the Atomicintegerarray, Atomiclongarray of the array operation.The initial understanding of the thread is DEMO2 because of the security issues caused by atomic operations. The use of Atomicinteger can be solved. Public classDemo2 {PrivateAtomicinteger value =NewAtomicinteger (1);
Objective-C keywords: retain, assgin, copy, readonly, atomic, nonatomic, objective-cassginUse of declarative attributes: declarative attributes are called the compile-time syntax @ property (retain, nonatomic) Some * s; @ property (parameter 1, parameter 2) Some * s; parameter 1: retain: Modify reference (object) Data Type assgin: Modify basic data type (default) copy: Some objects need to be copied to use NSString readonly: Read-only, only setter met
The Java class library includes a number of useful "base module" classes. In general, we should prefer to reuse these existing classes instead of creating new ones. : Reuse can reduce development effort, develop risk (because existing classes are already tested), and maintenance costs. Sometimes, a thread-safe class can support all the operations we need, but many other times, the existing class can only support most of the operations, and it is necessary to add a new operation without breaking
What operations does Linux support have atomic characteristics? Knowing these things is the basis for understanding and designing a non-locking programming algorithm.__sync_fetch_and_add series of commands, found this series of commands to say the best of an article, English good students can go directly to the original text. Multithreaded simple data type access and atomic variables__sync_fetch_and_add Ser
In a multithreaded environment, access to shared variables can be implemented using the lock free technology based on compare and swap, the benefit of which is high efficiency.An extract of atomic operation1.1 AndroidSource: system/core/libcutils/atomic.c (for X86):1#elif defined (__i386__) | | Defined (__x86_64__)23void Android_atomic_write (int32_t value,Volatile int32_t*Addr) {4int32_t OldValue;5Do{6 OldValue = *Addr7}While(Android_atomic_cmpxchg (
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.