zoukankan      html  css  js  c++  java
  • cas

     1 http://blog.163.com/xychenbaihu@yeah/blog/static/1322296552013373236132/
     2 
     3 https://www.cnblogs.com/wzjhoutai/p/6838104.html
     4 
     5  
     6 
     7  
     8 
     9 #include<stdio.h>
    10 #include<string.h>
    11 #include<stdlib.h>
    12 #include<pthread.h>
    13 
    14 int count = 0;
    15 void *func_cas(void*arg)
    16 {
    17     int i=0;
    18     int temp = 0;
    19 
    20     for(i = 0;i < 20000;i++)
    21     {
    22         while(1)
    23         {
    24             temp = count;
    25             if(__sync_bool_compare_and_swap(&count, temp, temp+1))
    26                 break;
    27         }
    28 
    29     }
    30 }
    31 
    32 void *func(void*arg)
    33 {
    34     int i=0;
    35 
    36     for(i = 0;i < 20000;i++)
    37     {
    38         count++;
    39     }
    40 }
    41 
    42 
    43 int main()
    44 {
    45     pthread_t id[10];
    46     int i=0;
    47     for(i=0 ;i< sizeof(id)/sizeof(id[0]);i++)
    48     {
    49         pthread_create(&id[i],NULL,func_cas,NULL);
    50     }
    51 
    52     for(i=0 ;i< sizeof(id)/sizeof(id[0]);i++)
    53     {
    54         pthread_join(id[i],NULL);
    55     }
    56 
    57     printf("count = %d
    ",count);
    58 
    59     count = 0;
    60 
    61     for(i=0 ;i< sizeof(id)/sizeof(id[0]);i++)
    62     {
    63         pthread_create(&id[i],NULL,func,NULL);
    64     }
    65 
    66     for(i=0 ;i< sizeof(id)/sizeof(id[0]);i++)
    67     {
    68         pthread_join(id[i],NULL);
    69     }    
    70     printf("
    count = %d
    ",count);
    71 }
    72 
    73 
    74             
  • 相关阅读:
    Bootstrap-select 动态绑定值
    Centos 下载
    设置图片大小和旋转
    aolication全局传递参数
    Activity静态变量传递参数
    Activity传递参数
    Activity横屏后声明周期的变化
    Intent隐式意图
    Intent显示意图的四种跳转
    SharedPreferences简单的应用
  • 原文地址:https://www.cnblogs.com/elseliving/p/8168469.html
Copyright © 2011-2022 走看看