#include <iostream> #include <string> #include <list> #include <ctime> #include
<mutex> #include <atomic> #include <thread> #include <cassert> //
1. So called atomic operation , It's what you take “ The atom is the smallest , Indivisible minimum individual ” The meaning of , It means that when multiple threads access the same global resource , //
It can ensure that all other threads do not access the same resources at the same time . That is, it ensures that only one thread can access the resource at the same time . //
This is similar to the protection of mutex objects for access to shared resources , But atomic operations are closer to the bottom , So it's more efficient . //
2. In the new standard C++11, The concept of atomic operation is introduced , The new header file provides a variety of atomic operation data types , for example ,atomic_bool, atomic_int wait , //
If we operate on these types of shared resources in multiple threads , The compiler will ensure that these operations are atomic , in other words , Make sure that there is only one thread at any time //
Resources , The compiler will guarantee that , The correctness of multiple threads accessing this shared resource . This avoids the use of locks , Improved efficiency . //
3. Atomic operations are operations that are not interrupted by thread scheduling ; Once this operation is started , It runs to the end , There won't be any in the middle context switch( Switch to another thread ). //
Simulate click operation , One hundred users click at the same time 100000 second . Total number of server hits , The result should be 10000000. typedef void(*ClickFunc)();
static long s_clicked_count = 0; static std::atomic_long
s_atomic_clicked_count; static std::mutex s_mutex; static void Click1() { for
(long i = 0; i < 100000; ++i) { // Lock free access to global resources , Thread insecurity leads to inaccurate result of accumulation , About tens of milliseconds .
s_clicked_count += 1; } } static void Click2() { for (long i = 0; i < 100000;
++i) { // 4. Locking is a very time-consuming operation , Severe loss performance . // Lock access to global resources , Thread safety , The results are correct , But it took about two minutes .
s_mutex.lock(); s_clicked_count += 1; s_mutex.unlock(); } } static void
Click3() { // 5. The implementation of atomic operations is similar to ordinary data types , But it can ensure that the results are correct , Supply ratio mutex Better performance of equal locking mechanism , //
If we want to access the shared resources, we can use atomic data type , Then use this new equivalent data type in multithreaded programs . for (long i = 0; i < 1000000;
++i) { // Atomic data type , Thread safety , The results are correct , About two seconds . s_atomic_clicked_count += 1; } } static void
Test(ClickFunc click_func) { s_clicked_count = 0; s_atomic_clicked_count = 0;
clock_t start = clock(); const int kThreadCount = 100; std::thread
threads[kThreadCount]; for (int i = 0; i < kThreadCount; ++i) { threads[i] =
std::thread(click_func); } for (int i = 0; i < kThreadCount; ++i) {
threads[i].join(); } clock_t end = clock(); std::cout << end - start << "ms.
s_clicked_count: " << s_clicked_count; std::cout << " s_atomic_clicked_count: "
<< s_atomic_clicked_count << std::endl; } int main() { std::cout << " Lockless access : ";
Test(Click1); std::cout << " Atomic data type : "; Test(Click3); std::cout << " Lock access : ";
Test(Click2); return 0; }

Technology
©2019-2020 Toolsou All rights reserved,
Conflict statement (conflicting declaration) solve ToastUtils Use of Programmer and architect Hua Shan element ui Drop down box search function common 5 species JAVA Runtime exception STM32 Infrared tracking car Vue Development tips experiment 11-1-6 Output string at specified position (20 branch )java Several common runtime exceptions and simple examples Output string at specified position ( Detailed analysis )