C + + multithreading model and lock

Source: Internet
Author: User

C + + memory model

See "C++0x Ramble" series of: Before the multi-threaded memory model, where the thought of a multi-threaded can be so complex, all kinds of examples have seen after the three views of the destruction ... Programmers are right, is the compiler to optimize the trouble. Of course, the compiler also conforms to the previous standard, where is the problem? The standard for multithreading security is relatively missing. What exactly is the missing standard specifically? The above-mentioned article concludes with "sequential consistency" and does not give a detailed technical answer. And, I still have a doubt, if the above mentioned Pthread library and so on can not guarantee the correctness of multi-thread, so many people use no problem? No one's responding? How does the C + + multi-threaded windows/linux on the page be written?

Then, see the "self-accomplishment of the programmer" in the lock does not guarantee a thread-safe error this article. From the article and the following thread message discussion can be seen: Pthread Library has its own set of memory visibility specifications, so ensure the normal execution of the program. If the locking mechanism used does not guarantee this, there may be a problem with the said locking or the thread safety.

To the C++11 specific memory model, see C++11 Concurrency Guide VII (C++11 memory model One: Introduction)

We have to make certain constraints on the compiler and CPU to properly optimize your program, so what is this constraint? Answer: Memory model. C + + programmers want to write high-performance multithreaded programs must understand the memory model, the compiler will give your program to optimize (static), the CPU in order to improve performance is also disorderly execution (dynamic), in short, the program in the final execution will not follow your original code order to execute, so the memory model is the programmer, compiler, The contract between the CPUs, after adhering to the contract, we will optimize each other, so as to maximize the performance of the program. 6 types of c++11 are specified (Memory order)

But the specific six kinds of memory order how to use, the difference between the feeling is not clear, the key is no example, waiting to see a good article to add ~

Signal volume and Spin lock

After watching Chenhao's blog about the implementation of the lock-free queue, feel the use of CAs what's on the tall. But there is some confusion in the code--spin is a while loop--is it wasting CPU, can it be efficient? The previous few days to see C++11 has been supported, so I wrote a program to see exactly how efficient. So with Xcode wrote a demo contrast Atomic_flag and mutex contrast test. 100 threads, with each thread looping through the global variable 10,000 times. The use of spin lock execution time is much more than the use of mutexes, CPU utilization is very high, and the mutex CPU basic utilization is very low. Compared with the performance of spin lock and mutex in pthreads parallel programming, this article tests and explains the above phenomena, and presents the use scenarios of spin locks and mutexes:

Pthread_spin_lock should be the same as the spin lock inside the kernel, the key area of the lock is smaller. If one thread 1 obtains spin after another thread 2 busy-wait the spin lock procedure on another CPU, if thread 2 has not obtained the spin lock after running the CPU time, just note that the thread 1 lock takes too long. The mutex should be used instead of spin lock.

C + + multithreading model and lock

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.