linux线程学习(3)

线程的同步

1. 互斥量

  1. 为什么要使用互斥量:
    当多个线程共享相同的内存时,需要每一个线程看到相同的视图。当一个线程修改变量时,而其他线程也可以读取或者修改这个变量,就需要对这些线程同步,确保他们不会访问到无效的变量
  2. 互斥锁的初始化和销毁:
    为了让线程访问数据不产生冲突,这要就需要对变量加锁,使得同一时刻只有一个线程可以访问变量。互斥量本质就是锁,访问共享资源前对互斥量加锁,访问完成后解锁。当互斥量加锁以后,其他所有需要访问该互斥量的线程都将阻塞。当互斥量解锁以后,所有因为这个互斥量阻塞的线程都将变为就绪态,第一个获得cpu的线程会获得互斥量,变为运行态,而其他线程会继续变为阻塞,在这种方式下访问互斥量每次只有一个线程能向前执行
  3. 加锁和解锁:
    加锁
int pthread_mutex_lock(pthread_mutex_t *mutex); 
    //成功返回0,失败返回错误码。如果互斥量已经被锁住,那么会导致该线程阻塞
int pthread_mutex_trylock(pthread_mutex_t *mutex);
    //成功返回0,失败返回错误码。如果互斥量已经被锁住,不会导致线程阻塞

解锁

 int pthread_mutex_unlock(pthread_mutex_t *mutex);
    //成功返回0,失败返回错误码。如果一个互斥量没有被锁住,那么解锁就会出错

例子:创建两个线程,在线程中进行对全局变量for循环自加,观察有互斥锁和没有互斥锁的全局变量的结果

无互斥锁:

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>

//定义一全局变量
int val;

void *thread_fun(void *arg)
{
        int i;
        for(i=0;i<10000;i++)
        {
            val++;
        }
    return(void *)0;
}
int main()
{
    pthread_t tid;

    //创造新线程,这里创建两个新线程
    pthread_create(&tid, NULL, thread_fun, NULL);
    pthread_create(&tid, NULL, thread_fun, NULL);

    //等待新线程运行结束
    pthread_join(tid, NULL);
    printf("val=%d\n",val);//打印经过两次for循环后的val值,本应该是20000,但实际不是

    return 0;
}

这里写图片描述
运行结果不为20000

有互斥锁:

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>

//定义两个全局变量
int val;
pthread_mutex_t mutex;//这个为互斥量

void *thread_fun(void *arg)
{
        int i;
        //加锁,防止其他线程进行加锁,防止产生错乱
        pthread_mutex_lock(&mutex);
        for(i=0;i<10000;i++)
        {
            val++;
        }
        //一次循环完成,打印结果一次
        printf("一次循环完成,打印结果一次val=%d\n",val );
        pthread_mutex_unlock(&mutex);//解锁

    return(void *)0;
}

int main()
{
    pthread_t tid;
    int err;
    //对互斥量进行初始化,只有初始化过到互斥量才能使用
    err = pthread_mutex_init(&mutex, NULL);
    if(err != 0)
    {
        printf("init mutex failed\n");
        return;
    }
    //创造新线程,这里创建两个新线程
    pthread_create(&tid, NULL, thread_fun, NULL);
    pthread_create(&tid, NULL, thread_fun, NULL);
    //等待新线程运行结束

    pthread_join(tid, NULL);
    printf("最后得到的值val=%d\n",val);//打印经过两次for循环后的val值

    return 0;
}

运行结果:
这里写图片描述

2. 读写锁

  • 什么是读写锁,它与互斥锁的区别:

    • 读写锁与互斥量类似,不过读写锁有更高的并行性。互斥量要么加锁要么不加锁,而且同一时刻只允许一个线程对其加锁。对于一个变量的读取,完全可以让多个线程同时进行操作
    • pthread_rwlock_t rwlock
      读写锁有三种状态,读模式下加锁,写模式下加锁,不加锁。一次只有一个线程可以占有写模式下的读写锁,但是多个线程可以同时占有读模式的读写锁
    • 读写锁在写加锁状态时,在它被解锁之前,所有试图对这个锁加锁的线程都会阻塞(无论是读加锁还是写加锁)。读写锁在读加锁状态时,所有试图以读模式对其加锁的线程都会获得访问权,但是如果线程希望以写模式对其加锁,它必须阻塞直到所有的线程释放锁
    • 当读写锁读模式加锁时,如果有线程试图以写模式对其加锁,那么读写锁会阻塞随后的读模式锁请求。这样可以避免读锁长期占用,而写锁达不到请求。
    • 读写锁非常适合对数据结构读次数大于写次数的程序,当它以读模式锁住时,是以共享的方式锁住的;当它以写模式锁住时,是以独占的模式锁住的。
      例子:创建两个线程,分为以下三种情况:①线程一读加锁,线程二写加锁。②线程一读加锁,线程二读加锁。

情况①

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>


pthread_rwlock_t rwlock;//定义一个读写锁
int num=0;

void *thread_fun1(void *arg)
{
    int err;

    pthread_rwlock_rdlock(&rwlock);//读模式下加锁
    printf("thread 1 is begin\n");
    sleep(3);
    printf("thread 1 over\n");

    pthread_rwlock_unlock(&rwlock);

    return (void *)1;
}

void *thread_fun2(void *arg)
{
    int err;

    pthread_rwlock_wrlock(&rwlock);//写模式下加锁
    printf("thread 2 is begin\n");
    sleep(3);
    printf("thread 2 is over\n");
    pthread_rwlock_unlock(&rwlock);

    return (void *)1;
}

int main()
{
    pthread_t tid1, tid2;
    int err;

    err = pthread_rwlock_init(&rwlock, NULL);//初始化读写锁
    if(err)
    {
        printf("init rwlock failed\n");
        return ;
    }

    err = pthread_create(&tid1, NULL, thread_fun1, NULL);//创建新线程1
    if(err)
    {
        printf("create new thread 1 failed\n");
        return ;
    }
    err = pthread_create(&tid2, NULL, thread_fun2, NULL);//创建新线程2
    if(err)
    {
        printf("create new thread 2 failed\n");
        return ;
    }

    pthread_join(tid1, NULL);//连接线程,等待线程运行结束
    pthread_join(tid2, NULL);

    pthread_rwlock_destroy(&rwlock);//销毁读写锁

    return 0;
}

运行结果:
这里写图片描述
第一次运行:读写锁在读加锁状态时,另外线程写模式对其加锁,它必须阻塞到读加锁线程释放锁。第二次运行:读写锁在写加锁状态时,在它被解锁之前,所有试图对这个锁加锁(无论是读加锁还是写加锁)的线程都会阻塞。

情况②:将线程二的启动函数代码改为:

void *thread_fun2(void *arg)
{
    int err;

    pthread_rwlock_rdlock(&rwlock);//读模式下加锁
    printf("thread 2 is begin\n");
    sleep(3);
    printf("thread 2 is over\n");
    pthread_rwlock_unlock(&rwlock);

    return (void *)1;
}

运行结果:
这里写图片描述
读写锁在读加锁状态时,所有试图以读模式对其加锁的线程都会获得访问权

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值