108 odd tricks in Linux programming-1 (false sharing)

Source: Internet
Author: User

I plan to open a long series of blogs. I expect 108 articles. I hope it will be completed within 1-2 years.

 

Note: Since I have been using Linux for development for a long time, some codes cannot be compiled in Windows or may cause problems. We recommend that you use the Linux environment for experiments, preferably with the 2.6 kernel, the processor must be multi-core. Many readers say that I am talking on paper. This is really bad. From the beginning of this series, there have basically been about 50 lines of code. This series does not represent any academic or industrial standpoint. It is only my personal interests and interests. Due to the limited level, errors are inevitable. Please do not expect too much.

 

I will write the first article as follows:

 

The following code is compiled into two programs respectively. It is only the difference in variable definition. The execution time difference is huge. Why?

This blog will not be decrypted for the time being. After a few days, I will write the second half. I hope readers will experiment and answer questions.

 

# Include <stdio. h>
# Include <pthread. h>
# Include <string. h>
# Include <stdlib. h>
# Ifdef FS
Size_t cnt_1;
Size_t cnt_2;
# Endif
# Ifdef nonfs

Size_t _ attribute _ (aligned (64) cnt_1;
Size_t _ attribute _ (aligned (64) cnt_2;
# Endif
Void * sum1 (void *)
{
For (INT I = 0; I <10000000; ++ I ){
Cnt_1 + = 1;
}
};
Void * sum2 (void *)
{
For (INT I = 0; I <10000000; ++ I ){
Cnt_2 + = 1;
}
};
Int main ()
{
Pthread_t * thread = (pthread_t *) malloc (2 * sizeof (pthread_t ));
Pthread_create (& Thread [0], null, sum1, null); // create two threads to sum
Pthread_create (& Thread [1], null, sum2, null );
Pthread_join (thread [0], null); // wait for two threads to end computing.
Pthread_join (thread [1], null );
Free (thread );
Printf ("cnt_1: % d, cnt_2: % d", cnt_1, cnt_2 );
}
Compilation Method:
G ++ fs. cpp-O test_nfs-G-d nonfs-lpthread
G ++ fs. cpp-O test_fs-G-d FS-lpthread

When using time./test_nfs and time./test_fs, you will find that the execution time is very different. Please reply with us. Thank you.

 

For more information, see: http://blog.csdn.net/pennyliang/archive/2010/10/26/5966433.aspx

 

 

Linyai, a netizen, has made an experiment. You can refer to the experiment below.

 

 

Linyai posted on Thu Oct 21 2010 10:11:22 GMT + 0800 (China Standard Time) report reply deleted
FS: 0m0. 083 s NFS: 0m0. 043 s FS: 0m0. 130 s NFS: 0m0. 034 s FS: 0m0. 084 s NFS: 0m0. 037 s FS: 0m0. 086 s NFS: 0m0. 042 s

 

 

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.