linux多线程之自旋锁

转载自 http://blog.csdn.net/daiyudong2020/article/details/52202526


基本概念:

何谓自旋锁?它是为实现保护共享资源而提出一种锁机制。其实,自旋锁与互斥锁比较类似,它们都是为了解决对某项资源的互斥使用。无论是互斥锁,还是自旋锁,在任何时刻,最多只能有一个保持者,也就说,在任何时刻最多只能有一个执行单元获得锁。但是两者在调度机制上略有不同。对于互斥锁,如果资源已经被占用,资源申请者只能进入睡眠状态。但是自旋锁不会引起调用者睡眠,如果自旋锁已经被别的执行单元保持,调用者就一直循环在那里看是否该自旋锁的保持者已经释放了锁,"自旋"一词就是因此而得名。


一、初始化和销毁锁

[cpp]  view plain  copy
  1. PTHREAD_SPIN_DESTROY(P)    POSIX Programmer's Manual   PTHREAD_SPIN_DESTROY(P)  
  2.   
  3. NAME  
  4.        pthread_spin_destroy,  pthread_spin_init - destroy or initialize a spin  
  5.        lock object (ADVANCED REALTIME THREADS)  
  6.   
  7. SYNOPSIS  
  8.        #include <pthread.h>  
  9.   
  10.        int pthread_spin_destroy(pthread_spinlock_t *lock);  
  11.        int pthread_spin_init(pthread_spinlock_t *lock, int pshared);  
两个函数的返回值:若成功,返回0;否则,返回错误编号
关于pthread_spin_init函数的参数pshard,单进程可以设置成PTHREAD_PROCESS_SHARED


二、加锁与解锁

[cpp]  view plain  copy
  1. PTHREAD_SPIN_LOCK(P)       POSIX Programmer's Manual      PTHREAD_SPIN_LOCK(P)  
  2.   
  3. NAME  
  4.        pthread_spin_lock,  pthread_spin_trylock  -  lock  a  spin  lock object  
  5.        (ADVANCED REALTIME THREADS)  
  6.   
  7. SYNOPSIS  
  8.        #include <pthread.h>  
  9.   
  10.        int pthread_spin_lock(pthread_spinlock_t *lock);  
  11.        int pthread_spin_trylock(pthread_spinlock_t *lock);  
  12.        int pthread_spin_unlock(pthread_spinlock_t *lock);  
两个函数的返回值:若成功,返回0;否则,返回错误编号

例子1,互斥锁的耗时, gcc pthread_mutex.c -pthread -o mutex:

[cpp]  view plain  copy
  1. #include <pthread.h>  
  2. #include <stdio.h>  
  3. #include <unistd.h>  
  4. #include <stdlib.h>  
  5. #include <errno.h>  
  6. #include <sys/timeb.h>  
  7.   
  8. static int num = 0;  
  9. static int count = 10000000;  
  10. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;  
  11.   
  12. void Perror(const char *s)  
  13. {  
  14.     perror(s);  
  15.     exit(EXIT_FAILURE);  
  16. }  
  17.   
  18. long long getSystemTime() {  
  19.     struct timeb t;  
  20.     ftime(&t);  
  21.     return 1000 * t.time + t.millitm;  
  22. }  
  23.   
  24. void* fun2(void *arg)  
  25. {  
  26.     pthread_t thread_id = pthread_self();  
  27.     printf("the thread2 id is %ld\n", (long)thread_id);  
  28.     int i = 1;  
  29.     for (; i<=count; ++i) {  
  30.         pthread_mutex_lock(&mutex);  
  31.         num += 1;  
  32.         pthread_mutex_unlock(&mutex);  
  33.     }  
  34. }  
  35.   
  36. int main()  
  37. {  
  38.     int err;  
  39.     pthread_t thread1;  
  40.     pthread_t thread2;  
  41.   
  42.     thread1 = pthread_self();  
  43.     printf("the thread1 id is %ld\n", (long)thread1);  
  44.   
  45.     long long start = getSystemTime();  
  46.   
  47.     // Create thread  
  48.     err = pthread_create(&thread2, NULL, fun2, NULL);  
  49.     if (err != 0) {  
  50.         Perror("can't create thread2\n");  
  51.     }  
  52.   
  53.     int i = 1;  
  54.     for (; i<=count; ++i) {  
  55.         pthread_mutex_lock(&mutex);  
  56.         num += 1;  
  57.         pthread_mutex_unlock(&mutex);  
  58.     }  
  59.   
  60.     pthread_join(thread2, NULL);  
  61.     long long end = getSystemTime();  
  62.   
  63.     printf("The num is %d, pay %lld ms\n", num, (end-start));  
  64.   
  65.     return 0;  
  66. }  

例子2,自旋锁的耗时,gcc pthread_spin.c -pthread -o spin:

[cpp]  view plain  copy
  1. #include <pthread.h>  
  2. #include <stdio.h>  
  3. #include <unistd.h>  
  4. #include <stdlib.h>  
  5. #include <errno.h>  
  6. #include <sys/timeb.h>  
  7.   
  8. static int num = 0;  
  9. static int count = 10000000;  
  10. static pthread_spinlock_t spin;  
  11.   
  12. void Perror(const char *s)  
  13. {  
  14.     perror(s);  
  15.     exit(EXIT_FAILURE);  
  16. }  
  17.   
  18. long long getSystemTime() {  
  19.     struct timeb t;  
  20.     ftime(&t);  
  21.     return 1000 * t.time + t.millitm;  
  22. }  
  23.   
  24. void* fun2(void *arg)  
  25. {  
  26.     pthread_t thread_id = pthread_self();  
  27.     printf("the thread2 id is %ld\n", (long)thread_id);  
  28.     int i = 1;  
  29.     for (; i<=count; ++i) {  
  30.         pthread_spin_lock(&spin);  
  31.         num += 1;  
  32.         pthread_spin_unlock(&spin);  
  33.     }  
  34. }  
  35.   
  36. int main()  
  37. {  
  38.     int err;  
  39.     pthread_t thread1;  
  40.     pthread_t thread2;  
  41.   
  42.     pthread_spin_init(&spin, PTHREAD_PROCESS_PRIVATE);  
  43.   
  44.     thread1 = pthread_self();  
  45.     printf("the thread1 id is %ld\n", (long)thread1);  
  46.   
  47.     long long start = getSystemTime();  
  48.   
  49.     // Create thread  
  50.     err = pthread_create(&thread2, NULL, fun2, NULL);  
  51.     if (err != 0) {  
  52.         Perror("can't create thread2\n");  
  53.     }  
  54.   
  55.     int i = 1;  
  56.     for (; i<=count; ++i) {  
  57.         pthread_spin_lock(&spin);  
  58.         num += 1;  
  59.         pthread_spin_unlock(&spin);  
  60.     }  
  61.   
  62.     pthread_join(thread2, NULL);  
  63.     long long end = getSystemTime();  
  64.   
  65.     printf("The num is %d, pay %lld ms\n", num, (end-start));  
  66.     pthread_spin_destroy(&spin);  
  67.     return 0;  
  68. }  

运行结果,可以看到某些条件下,自旋锁是比较快的:

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值