linux多线程——互斥量实现同步

一、互斥锁

互斥锁本质就是一个特殊的全局变量,拥有lock和unlock两种状态,unlock的互斥锁可以由某个线程获得,当互斥锁由某个线程持有后,这个互斥锁会锁上变成lock状态,此后只有该线程有权力打开该锁,其他想要获得该互斥锁的线程都会阻塞,直到互斥锁被解锁。

例子中使用静态初始化互斥量,也可以调用函数动态初始化。使用到的函数有pthread_mutex_lock,给资源上锁,和mutex_unlock,给资源解锁,可以实现同步访问,避免竞争。

在下面的结果也可看到,一个线程执行完循环后,另一个线程才会继续进行。如果删除加减锁过程、那么线程0,1会交替的执行。 

PTHREAD_MUTEX_NORMAL默认锁

// 静态方式创建互斥锁
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 

// 动态方式创建互斥锁,其中参数mutexattr用于指定互斥锁的类型,如果为NULL,就是普通锁。
int pthread_mutex_init (pthread_mutex_t* mutex,const pthread_mutexattr_t* mutexattr);

int pthread_mutex_lock(pthread_mutex_t *mutex); 
int pthread_mutex_trylock(pthread_mutex_t *mutex); // 尝试加锁,非阻塞
int pthread_mutex_unlock(pthread_mutex_t *mutex); 
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<pthread.h>

pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
void *func(void *arg)
{
    pthread_mutex_lock(&mutex);
    int threadno=*(int *)arg;
    int i=0;
    for(i;i<10;i++)
    {
        printf("%dthread:%d\n",threadno,i);
        sleep(1);
    }
    pthread_mutex_unlock(&mutex);
    pthread_exit((void *)threadno);
}

int main()
{
    pthread_t tid0,tid1;
    int i0=0,i1=1;
    pthread_create(&tid0,NULL,func,&i0);
    pthread_create(&tid1,NULL,func,&i1);
    pthread_join(tid0,NULL);
    pthread_join(tid1,NULL);
    printf("主线程退出\n");
    return 0;
}

 

二、自旋锁的使用

自旋锁就是一个线程如果没有获得这个锁的话,他还是会占用CPU,不停的尝试获得锁,如果自旋锁能够很快被释放,那么性能就会很高,如果自旋锁长时间不能够被释放,那么会大大降低CPU性能。不能及时的感受到是否有可用锁,需要循环着尝试来检测是否可以获得锁。 

申请一个自旋锁是pthread_spinlock_t lock。使用的函数有pthread_spin_lock(&lock),这个是上锁。pthread_spin_unlock(&lock)这个是给lock解锁。sched_yield()是自身线程放弃cpu的使用权,让一个准备好的线程占用cpu。

int pthread_spin_init(pthread_spinlock_t *lock, int pshared); // 创建自旋锁

int pthread_spin_lock(pthread_spinlock_t *lock); // 加锁,阻塞
int pthread_spin_trylock(pthread_spinlock_t *lock); // 尝试加锁,非阻塞
int pthread_spin_unlock(pthread_spinlock_t *lock); // 解锁

可以看到代码的运行过程也是正确的,

  1 #include<stdio.h>
  2 #include<unistd.h>
  3 #include<stdlib.h>
  4 #include<pthread.h>
  5 
  6 int money=50;
  7 pthread_spinlock_t lock;
  8  
  9 void *func(void *arg)
 10 {
 11     int num=*(int *)arg;
 12     while(money>0)
 13     {
 14         pthread_spin_lock(&lock);
 15         if(money>0)
 16         {
 17             money--;
 18             printf("%dtoke one money,last %d \n",num,money);
 19         }
 20         pthread_spin_unlock(&lock);
 21         sched_yield();    //这是线程主动放弃cpu的使用权,让其他线程执行。
 22         sleep(1);
 23     }
 24 }
 25 int main()
 26 {
 27     pthread_spin_init(&lock,0);
 28     pthread_t tid0,tid1,tid2;
 29     int t0=1,t1=2,t2=3;
 30     void *ret;
 31 
 32      pthread_create(&tid0,NULL,func,&t0);
 33      pthread_create(&tid2,NULL,func,&t2);
 34      pthread_create(&tid1,NULL,func,&t1);
 35 
 36 
 37     pthread_join(tid0,NULL);
 38     pthread_join(tid1,NULL);
 39     pthread_join(tid2,NULL);
 40 
 41     printf("主线程完成\n");
 42     return 0;
 43 }

 三、信号量

信号量是一个计数器,用于控制访问有限共享资源的线程数。

// 创建信号量
// pshared:一般取0,表示调用进程的信号量。非0表示该信号量可以共享内存的方式,为多个进程所共享(Linux暂不支持)。
// value:信号量的初始值,可以并发访问的线程数。
int sem_init (sem_t* sem, int pshared, unsigned int value);

int sem_wait (sem_t* sem); // 信号量减1,信号量为0时就会阻塞

int sem_trywait (sem_t* sem); // 信号量减1,信号量为0时返回-1,不阻塞

int sem_timedwait (sem_t* sem, const struct timespec* abs_timeout); // 信号量减1,信号量为0时阻塞,直到abs_timeout超时返回-1

int sem_post (sem_t* sem); // 信号量加1

使用方式和以上代码基本相同。

四、总结

多线程同步的方法还有许多种,后面加上一些比较有难度的代码学习。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西邮小菜机

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值