1. /*
    2. * =====================================================================================
    3. *
    4. * Filename: atomic.c
    5. *
    6. * Description: gcc-4.1.1: Built-in functions for atomic memory access
    7. * http://gcc.gnu.org/onlinedocs/gcc-4.1.1/gcc/Atomic-Builtins.html
    8. compile:
    9. gcc atomic.c -lpthead
    10. run:
    11. time ./a.out
    12. modify Macro VER to choose the version
    13. 1. no lock
    14. 2. pthread_mutex
    15. 3. atomic
    16. ref:
    17. http://gcc.gnu.org/onlinedocs/gcc-4.1.1/gcc/Atomic-Builtins.html
    18. http://www.alexonlinux.com/multithreaded-simple-data-type-access-and-atomic-variables
    19. http://www.cnblogs.com/FrankTan/archive/2010/12/11/1903377.html
    20. the demo, create 4 threads. the odd thread do --, the even threads do ++.
    21. so, the global_int should be zero. we can see:
    22. 1. the "no lock version", fastest but not thread safe
    23. 2. the "pthread mutex version" slowest, but thread safe
    24. 3. the "atomic version", fast and thread safe
    25. * Version: 1.0
    26. * Created: 09/11/2012 05:28:13 PM
    27. * Revision: none
    28. * Compiler: gcc
    29. *
    30. * Author: TED (cn), guolb57@163.com
    31. *
    32. * ===================================================================================
    33. */
    34. #include <stdio.h>
    35. #include <pthread.h>
    36. #include <unistd.h>
    37. #include <stdlib.h>
    38. #include <errno.h>
    39. /************************** for test the time cost begin *********************/
    40. #include <sys/time.h>
    41. struct timeval tp1, tp2;
    42. static void start_timer()
    43. {
    44. gettimeofday(&tp1, NULL);
    45. }
    46. /* return by seconds */
    47. static double end_timer()
    48. {
    49. gettimeofday(&tp2, NULL);
    50. return((tp2.tv_sec - tp1.tv_sec) +
    51. (tp2.tv_usec - tp1.tv_usec) * 1.0e-6);
    52. }
    53. /* for test the time cost end */
    54. #define LOOP_NUM 1000000
    55. int global_int = 0;
    56. #define VER 3 /* modify VER to choose the version here */
    57. #define NO_LOCK_VER 1 /* quickest & not thread safe */
    58. #define MUTEX_VER 2 /* slowest & thread safe */
    59. #define ATOMIC_VER 3 /* quick & thread safe */
    60. #if (VER == MUTEX_VER)
    61. pthread_mutex_t lock;
    62. #endif
    63. void *thread_proc( void *arg )
    64. {
    65. int i;
    66. int flag = (int)arg;
    67. for (i = 0; i < LOOP_NUM; i++) {
    68. if(flag % 2 == 0){
    69. #if (VER == NO_LOCK_VER)
    70. global_int++;
    71. #elif (VER == ATOMIC_VER)
    72. __sync_fetch_and_add( &global_int, 1 );
    73. #elif (VER == MUTEX_VER)
    74. pthread_mutex_lock(&lock);
    75. global_int++;
    76. pthread_mutex_unlock(&lock);
    77. #endif
    78. }
    79. else{
    80. #if (VER == NO_LOCK_VER)
    81. global_int--;
    82. #elif (VER == ATOMIC_VER)
    83. __sync_fetch_and_sub(&global_int, 1);
    84. #elif (VER == MUTEX_VER)
    85. pthread_mutex_lock(&lock);
    86. global_int--;
    87. pthread_mutex_unlock(&lock);
    88. #endif
    89. }
    90. }
    91. return NULL;
    92. }
    93. int main(int argc, const char *argv[])
    94. {
    95. int i;
    96. int thread_num = 4;
    97. pthread_t *pthread_id;
    98. #if (VER == MUTEX_VER)
    99. pthread_mutex_init(&lock,NULL);
    100. #endif
    101. int ntimes = 100;
    102. start_timer();
    103. while(ntimes--){
    104. pthread_id = (pthread_t*)malloc( sizeof( pthread_t ) * thread_num);
    105. if (NULL == pthread_id) {
    106. perror( "malloc" );
    107. exit(1);
    108. }
    109. for (i = 0; i < thread_num; i++){
    110. if (pthread_create( &pthread_id[i], NULL, thread_proc, (void*)i)){
    111. perror( "pthread_create" );
    112. exit(1);
    113. }
    114. }
    115. for (i = 0; i < thread_num; i++){
    116. pthread_join(pthread_id[i], NULL);
    117. }
    118. free(pthread_id);
    119. printf( "global_int value is: %d\n", global_int );
    120. printf( "The Value should be: 0\n");
    121. global_int = 0;
    122. }
    123. printf("cost %g\n", (double)end_timer());
    124. return 0;
    125. }

    原文: