Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2298729
  • 博文数量: 395
  • 博客积分: 10994
  • 博客等级: 上将
  • 技术积分: 5586
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-17 19:49
文章存档

2014年(1)

2013年(10)

2012年(74)

2011年(303)

2010年(7)

分类: LINUX

2012-02-18 16:46:12

    1. 使用pthread_mutex_t锁
    2. 内容出自:http://hi.baidu.com/feng2211/blog/item/3062e7fa3e230f13a9d3116a.html

    3. inux下为了多线程同步,通常用到锁的概念。
    4. posix下抽象了一个锁类型的结构:ptread_mutex_t。通过对该结构的操作,来判断资源是否可以访问。顾名思义,加锁(lock)后,别人就无法打开,只有当锁没有关闭(unlock)的时候才能访问资源。
    5. 它主要用如下5个函数进行操作。
    6. 1:pthread_mutex_init(pthread_mutex_t * mutex,const pthread_mutexattr_t *attr);
    7. 初始化锁变量mutex。attr为锁属性,NULL值为默认属性。
    8. 2:pthread_mutex_lock(pthread_mutex_t *mutex);加锁
    9. 3:pthread_mutex_tylock(pthread_mutex_t *mutex);加锁,但是与2不一样的是当锁已经在使用的时候,返回为EBUSY,而不是挂起等待。
    10. 4:pthread_mutex_unlock(pthread_mutex_t *mutex);释放锁
    11. 5:pthread_mutex_destroy(pthread_mutex_t *mutex);使用完后释放

    12. 使用pthread_mutex_t锁,下面经典例子为创建两个线程对sum从1加到100。前面第一个线程从1-49,
    13. 后面从50-100。主线程读取最后的加值。为了防止资源竞争,用了pthread_mutex_t 锁操作。
    14.                                                                                                                                     
    15. #include<stdlib.h>
    16. #include<stdio.h>
    17. #include<unistd.h>
    18. #include<pthread.h>
    19. #include <sys/syscall.h>
    20. #include <sys/types.h>
    21.                                    
    22. typedef struct ct_sum
    23. {
    24.       int sum;
    25.       pthread_mutex_t lock;
    26.  }ct_sum;
    27.                                                                                                                                                                                                                             
    28. void * add1(void * cnt)
    29. {
    30.       int i;
    31.       pthread_mutex_lock(&(((ct_sum*)cnt)->lock));
    32.                                                                                                                                     
    33.       for( i=0;i<50;i++)
    34.       {
    35.             (*(ct_sum*)cnt).sum+=i;
    36.                                                                                                                                     
    37.        }
    38.       printf("ptid1 = %lu\n", pthread_self());
    39.       printf("add 1 sum %d\n",((ct_sum*)cnt)->sum);
    40.       pthread_mutex_unlock(&(((ct_sum*)cnt)->lock));
    41.                                    
    42.       pthread_exit(NULL);
    43.       return 0;
    44. }
    45. void * add2(void *cnt)
    46. {
    47.       int i;
    48.       cnt= (ct_sum*)cnt;
    49.                                   
    50.       pthread_mutex_lock(&(((ct_sum*)cnt)->lock));
    51.       for( i=50;i<101;i++)
    52.       { (*(ct_sum*)cnt).sum+=i;
    53.                                                                                                                                     
    54.       }
    55.       printf("ptid2 = %lu\n", pthread_self()); //pthread_self也可以得到线程id值
    56.       printf("add2 sum %d\n",((ct_sum*)cnt)->sum);
    57.       pthread_mutex_unlock(&(((ct_sum*)cnt)->lock));
    58.                                                                                                                                     
    59.       pthread_exit(NULL);
    60.       return 0;
    61. }
    62.                                
    63. int main(void)
    64. {
    65.       int i;
    66.       pthread_t ptid1,ptid2;
    67.       int sum=0;
    68.       ct_sum cnt;
    69.       pthread_mutex_init(&(cnt.lock),NULL);
    70.       cnt.sum=0;
    71.       pthread_create(&ptid1,NULL,add1,&cnt);
    72.       pthread_create(&ptid2,NULL,add2,&cnt);
    73.                                                                                                                                     
    74.       pthread_mutex_lock(&(cnt.lock));
    75.       printf("ptid1 = %lu\n",ptid1); //注意:这个地方的那个%lu一定要注意,因为pthread_t是unsigned long型的,如果是%d的话,会得到负值
    76.       printf("ptid2 = %lu\n",ptid2);
    77.       printf("sum %d\n",cnt.sum);
    78.       pthread_mutex_unlock(&(cnt.lock));
    79.                                                                                                                                    
    80.       pthread_join(ptid1,NULL); //主线程等待
    81.       pthread_join(ptid2,NULL);
    82.       printf("sum %d\n",cnt.sum);
    83.       pthread_mutex_destroy(&(cnt.lock));
    84.       return 0;
    85. }
阅读(3130) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~