How to reduce context switching in concurrent programming

Source: Internet
Author: User
Tags switches

First, let's see what tools are available to measure the consumption of context switches.

Use LMBENCH3 (a profiling tool) to measure the duration of the context switch

Use Vmstat to measure the number of context switches.

For example: In the LUnix Command interface: Vmstat 2 1

CS (Content switch) indicates the number of context switches, and from the test results above we can see that the context switches 307 times per second.

Second, how to reduce the context switch

The methods for reducing context switching are lock-free concurrent programming, CAS algorithms, using minimal threads, and using the coprocessor.

1. No lock concurrent concurrent programming. A multi-threaded competition lock causes context switching, so when multithreading data is processed,

There are some ways to avoid using locks, such as taking the ID of the data according to the hash algorithm, and different threads dealing with different pieces of data.

2. CAS algorithm. The Java atomic package uses the CAS algorithm to update the data without requiring a lock.

Cas:compare and swap, is the meaning of comparison and exchange.

CAS has 3 operands, a memory value of V, an old expected value of a, and a new value to be modified B. If and only if the expected value A and the memory value of the V phase, the memory value of V is modified to B, otherwise do nothing.

Http://www.cnblogs.com/onlywujun/articles/3529572.html

3. Use a minimum number of threads. Avoid creating unwanted threads, such as fewer tasks, but creating a lot of threads to handle, which can cause a lot of threads to wait.

4. Co-process: in a single thread to achieve multi-tasking scheduling, and in a single thread to maintain a number of tasks between the switch.

Source: The Art of Java concurrent programming

It technology and industry exchange Group 417691667

How to reduce context switching in concurrent programming

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.