Atomic operations in the C++11

Source: Internet
Author: User

The so-called atomic operation, which is the meaning of the "atom is the smallest, indivisible minimum," means that when multiple threads access the same global resource, it is possible to ensure that all other threads do not access the same resources at the same time. That is, he ensured that only a single thread could access the resource at the same time. This is somewhat similar to the protection of the mutex object's access to the shared resource, but the atomic operation is closer to the bottom and thus more efficient.

Atomic operations are not regulated in previous C + + standards, and we tend to use assembly language, or with third-party line libraries, such as Intel's Pthread. In the new standard c++11, the concept of atomic operation was introduced, and through this new header file a variety of atomic manipulation data types were provided, such as Atomic_bool,atomic_int and so on, if we operate on these types of shared resources in multiple threads, The compiler will ensure that these operations are atomic, that is, to ensure that only one thread accesses the resource at any given time, the compiler guarantees that multiple threads will access the shared resource correctly. This avoids the use of locks and improves efficiency.

Let's look at a practical example. If we are going to design an ad click Statistics Program, in a server program, use multiple threads to simulate multiple user clicks on an ad:

#include <boost/thread/thread.hpp>
#include <atomic>
#include <iostream>
#include <time.h>

using namespace Std;
Global result data
Long total = 0;

Click function
void Click ()
{
for (int i=0; i<1000000;++i)
{
Lock-free access to global data
Total + = 1;
}
}


int main (int argc, char* argv[])
{
Timing starts
clock_t start = clock ();
Create 100 threads to simulate click Statistics
Boost::thread_group threads;
for (int i=0; i<100;++i)
{
Threads.create_thread (click);
}

Threads.join_all ();
End of timing
clock_t finish = clock ();
Output results
cout<< "Result:" <<total<<endl;
cout<< "duration:" <<finish-start<< "MS" <<endl;
return 0;
}

From the results of execution, such a method is very fast, but the result is not correct
E:sourcecodemingw>thread.exe
result:87228026
Duration:528ms

Naturally, we would have thought of using a mutex to protect access to a global shared resource, with the following implementation:

Long total = 0;
Mutex objects that are protected against shared resources
Mutex m;

void Click ()
{
for (int i=0; i<1000000;++i)
{
Lock mutex object before access
M.lock ();
Total + = 1;
When the access is complete, release the mutex object
M.unlock ();
}
}
The use of mutually exclusive objects, to ensure that only one thread at a time to access the share, from the execution of the results, mutually exclusive objects to ensure the correctness of the results, but also has a very large performance loss, from just 528ms into the present 8431, with the original time of 10 times. The loss is big enough.
E:sourcecodemingw>thread.exe
result:100000000
Duration:8431ms

If this is the end of our solution before c++11, however, C + + 's quest for performance is endless, and he always wants to do everything he can to squeeze the performance out of the CPU. In c++11, the data type of atomic operation (Atomic_bool,atomic_int,atomic_long and so on) is implemented, and the access to shared resources of these atomic data types can be achieved without the use of lock mechanism such as mutex, and the correct access to the share resource is realized.

Introduce the header file of the atomic data type
#include <atomic>

Use atomic data types as data types for shared resources
Atomic_long Total (0);
Long total = 0;

void Click ()
{
for (int i=0; i<1000000;++i)
{
Only the data type is different, and the form of access to it is no different from the resources of the ordinary data type.
Total + = 1;
}
}

Let's take a look at the effect of using atomic data types:
E:sourcecodemingw>thread.exe
result:100000000
Duration:2105ms

The results are correct! Time consuming is only one-fourth of mutex mutex objects used! It is only 4 times times as long as it does not take any protective mechanism. It can be said that this is a very good result.

The implementation of atomic operations is similar to the normal data type, but it can provide better performance than the lock mechanism, such as mutex, if the shared resources we want to access can be represented by atomic data types, it is a good choice to use this new equivalent data type in multithreaded programs.

Atomic operations in the C++11

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.