线程同步

1. 互斥量

问题:两个线程的指令交叉执行

解决:互斥量可以保证先后执行

原子性是指一系列操作不可被中断的特性。这一系列操作要么全部执行完成,要么全部没有执行

不存在部分执行,部分没有执行的情况。

互斥量是最简单的线程同步的方法

互斥量,处于两态之一的变量:解锁和加锁。这两个状态可以保证资源访问的串行。

操作系统直接提供了互斥量的API,开发者可以直接使用。

互斥量代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <vector>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; //互斥量,但是·会带来资源的损耗

int num = 0;
void *producer(void*) {
    int times = 10000000;
    while(times --) {
        pthread_mutex_lock(&mutex);
        num += 1;
        pthread_mutex_unlock(&mutex);
    }
    //return;
    return NULL;
}

void *comsumer(void*) {
    int times = 10000000;
    while(times --) {
        pthread_mutex_lock(&mutex);
        num -= 1;
        pthread_mutex_unlock(&mutex);
    }
    return NULL;
}
int main() {
    printf("start in main function.\n");
    pthread_t thread1, thread2;
    pthread_create(&thread1, NULL, &producer, NULL);
    pthread_create(&thread2, NULL, &comsumer, NULL);
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    printf("Print in main function num = %d\n", num);
    
    return 0;
}

2. 自旋锁

自旋锁是一种多线程同步的变量,使用自旋锁的线程会反复检查锁变量是否可用,但是自旋锁不会让出CPU,是一种忙等待状态

死循环等待锁被释放。

自旋锁的好处:

避免了进程或线程上下文切换的开销。操作系统内部好多地方使用自旋锁。自旋锁不适合在单CPU中使用。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <vector>
pthread_spinlock_t spinlock;

int num = 0;
void *producer(void*) {
    int times = 10000000;
    while(times --) {
        //pthread_mutex_lock(&mutex);
        pthread_spin_lock (&spinlock);
        num += 1;
        //pthread_mutex_unlock(&mutex);
        pthread_spin_unlock (&spinlock);
    }
    //return;
    return NULL;
}

void *comsumer(void*) {
    int times = 10000000;
    while(times --) {
        pthread_spin_lock (&spinlock);
        //pthread_mutex_lock(&mutex);
        num -= 1;
        //pthread_mutex_unlock(&mutex);
        pthread_spin_unlock (&spinlock);
    }
    return NULL;
}
int main() {
    printf("start in main function.\n");
    pthread_spin_init (&spinlock, 0);
    pthread_t thread1, thread2;
    pthread_create(&thread1, NULL, &producer, NULL);
    pthread_create(&thread2, NULL, &comsumer, NULL);
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    printf("Print in main function num = %d\n", num);
    
    return 0;
}
//这个代码在Xcode中不能运行,不知道为什么。

3. 读写锁

读写锁是一种特殊的自旋锁

允许多个读者同时访问资源以提高读性能

对于写操作是互斥的。

 所以理论来讲应该有两种锁。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <vector>
pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;

int num = 0;
void *write(void*) {
    int times = 10000000;
    while(times --) {
        pthread_rwlock_wrlock(&rwlock);
        //pthread_mutex_lock(&mutex);
        //pthread_spin_lock (&spinlock);
        num += 1;
        //pthread_mutex_unlock(&mutex);
        //pthread_spin_unlock (&spinlock);
        pthread_rwlock_unlock(&rwlock);
    }
    //return;
    return NULL;
}

void *read(void*) {
    int times = 10000000;
    
    while(times --) {
        pthread_rwlock_rdlock(&rwlock);
        //pthread_spin_lock (&spinlock);
        //pthread_mutex_lock(&mutex);
        //num -= 1;
        if(times % 1000 == 0) {
            //printf("print num in reader: num = %d\n",num);
            usleep(10);
        //pthread_mutex_unlock(&mutex);
        //pthread_spin_unlock (&spinlock);
        }
        pthread_rwlock_unlock(&rwlock);
        
    }
    return NULL;
}
int main() {
    printf("start in main function.\n");
    //pthread_spin_init (&spinlock, 0);
    pthread_t thread1, thread2, thread3;
    pthread_create(&thread1, NULL, &read, NULL);
    pthread_create(&thread2, NULL, &read, NULL);
    pthread_create(&thread3, NULL, &write, NULL);
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    pthread_join(thread3, NULL);
    printf("Print in main function num = %d\n", num);
    
    return 0;
}

4. 条件变量

缓冲区小于等于0时,不允许消费者消费,消费者必须等待。

缓冲区满时,不允许生产者往缓冲区生产,生产者必须等待。

当生产者生产一个产品时,唤醒可能等待的消费者。

当消费者消费一个产品时,唤醒可能等待的生产者。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <vector>
//pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; //互斥量,但是·会带来资源的损耗
int MAX_BUF = 100;
int num = 0;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; //互斥量,但是·会带来资源的损耗
void *producer(void*) {
    //int times = 10000000;
    while(true) {
        pthread_mutex_lock(&mutex);
        while(num >= MAX_BUF) {
            //wait
            pthread_cond_wait(&cond, &mutex);
            printf("缓冲区满了,等待消费者。。\n");
        }
        num += 1;
        
        printf("生产一个产品, 当前数量为:%d\n", num);
        sleep(1);
        pthread_cond_signal(&cond);
        printf("通知消费者。。\n");
        pthread_mutex_unlock(&mutex);
        sleep(0.5);
        
    }
    //return;
    return NULL;
}

void *comsumer(void*) {
    //int times = 10000000;
    while(true) {
        pthread_mutex_lock(&mutex);
        while(num <=0 ) {
            //wait
            pthread_cond_wait(&cond, &mutex);
            printf("缓冲区空了,等待生产者。。\n");
        }
        //pthread_mutex_lock(&mutex);
        num -= 1;
        
        printf("消费一个产品, 当前数量为:%d\n", num);
        sleep(1);
        pthread_cond_signal(&cond);
        printf("通知生产者。。\n");
        pthread_mutex_unlock(&mutex);
    }
    return NULL;
}
int main() {
    printf("start in main function.\n");
    pthread_t thread1, thread2;
    pthread_create(&thread1, NULL, &producer, NULL);
    pthread_create(&thread2, NULL, &comsumer, NULL);
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    printf("Print in main function num = %d\n", num);
    
    return 0;
}

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值