3.12生产者消费者模型 3.13条件变量 3.14信号量 C++实现生产者消费者模型

3.12生产者消费者模型

生产者消费者模型中的对象:
1、生产者
2、消费者
3、容器
若容器已满,生产者阻塞在这,通知消费者去消费;若容器已空,则消费者阻塞,通知生产者去生产。生产者可以有多个,消费者也可以有多个。容器中的数据是多个线程共享的,线程同步问题涉及到互斥量、读写锁等。
条件变量、信号量等。
在这里插入图片描述

/*
    生产者消费者模型(粗略的版本),此处不考虑容器存满
*/
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>

// 创建一个互斥量
pthread_mutex_t mutex;

struct Node{
    int num;
    struct Node *next;
};

// 头结点
struct Node * head = NULL;

void * producer(void * arg) {

    // 不断的创建新的节点(头插法),添加到链表中
    while(1) {
        pthread_mutex_lock(&mutex);
        //malloc默认返回void *,类型转换为struct Node *
        struct Node * newNode = (struct Node *)malloc(sizeof(struct Node));
        newNode->next = head;
        head = newNode;
        newNode->num = rand() % 1000;
        printf("add node, num : %d, tid : %ld\n", newNode->num, pthread_self());
        pthread_mutex_unlock(&mutex);
        usleep(100);
    }

    return NULL;
}

void * customer(void * arg) {

    while(1) {
        pthread_mutex_lock(&mutex);
        // 保存头结点的指针,记录要删除的结点信息
        struct Node * tmp = head;

        // 判断是否有数据,从头部开始删除
        if(head != NULL) {
            // 有数据
            head = head->next;
            printf("del node, num : %d, tid : %ld\n", tmp->num, pthread_self());
            free(tmp);
            pthread_mutex_unlock(&mutex);
            usleep(100);
        } else {
            // 没有数据,需进行解锁,不然讲产生死锁
            pthread_mutex_unlock(&mutex);
        }
    }
    return  NULL;
}

int main() {

    //初始化互斥量
    pthread_mutex_init(&mutex, NULL);

    // 创建5个生产者线程,和5个消费者线程
    pthread_t ptids[5], ctids[5];

    for(int i = 0; i < 5; i++) {
        pthread_create(&ptids[i], NULL, producer, NULL);
        pthread_create(&ctids[i], NULL, customer, NULL);
    }

    //分离后的子线程可自行释放资源,不需其他线程对其进行回收释放
    for(int i = 0; i < 5; i++) {
        pthread_detach(ptids[i]);
        pthread_detach(ctids[i]);
    }

	
    while(1) {
        sleep(10);
    }

    //释放互斥量
    pthread_mutex_destroy(&mutex);

    pthread_exit(NULL);

    return 0;
}

显示结果:
在这里插入图片描述
后续课程解决当前生产者消费者模型所存在的问题!互斥量、条件变量、信号量

3.13条件变量

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
条件变量,某个条件满足后引起阻塞或某个条件满足后解除阻塞。配合互斥锁实现线程同步。

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

// 创建一个互斥量,解决线程同步问题
pthread_mutex_t mutex;
// 创建条件变量,解决没有数据时,阻塞
pthread_cond_t cond;

struct Node{
    int num;
    struct Node *next;
};

// 头结点
struct Node * head = NULL;

void * producer(void * arg) {

    // 不断的创建新的节点,添加到链表中
    while(1) {
        pthread_mutex_lock(&mutex);
        struct Node * newNode = (struct Node *)malloc(sizeof(struct Node));
        newNode->next = head;
        head = newNode;
        newNode->num = rand() % 1000;
        printf("add node, num : %d, tid : %ld\n", newNode->num, pthread_self());
        
        // 只要生产了一个,就通知消费者消费,解除阻塞
        pthread_cond_signal(&cond);

        pthread_mutex_unlock(&mutex);
        usleep(100);
    }

    return NULL;
}

void * customer(void * arg) {

    while(1) {
        pthread_mutex_lock(&mutex);
        // 保存头结点的指针
        struct Node * tmp = head;
        // 判断是否有数据
        if(head != NULL) {
            // 有数据
            head = head->next;
            printf("del node, num : %d, tid : %ld\n", tmp->num, pthread_self());
            free(tmp);
            pthread_mutex_unlock(&mutex);
            usleep(100);
        } else {
            // 没有数据,需要等待
            // 当这个函数调用阻塞的时候,会对互斥锁进行解锁,当不阻塞的,继续向下执行,会重新加锁。
            pthread_cond_wait(&cond, &mutex);
            pthread_mutex_unlock(&mutex);
        }
    }
    return  NULL;
}

int main() {

    pthread_mutex_init(&mutex, NULL);
    pthread_cond_init(&cond, NULL);

    // 创建5个生产者线程,和5个消费者线程
    pthread_t ptids[5], ctids[5];

    for(int i = 0; i < 5; i++) {
        pthread_create(&ptids[i], NULL, producer, NULL);
        pthread_create(&ctids[i], NULL, customer, NULL);
    }

    //实现线程分离
    for(int i = 0; i < 5; i++) {
        pthread_detach(ptids[i]);
        pthread_detach(ctids[i]);
    }

    while(1) {
        sleep(10);
    }

    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond);

    pthread_exit(NULL);

    return 0;
}

在这里插入图片描述

3.14信号量

信号量,信号灯,灯亮则表示资源可用,灯灭则表示资源不可用。信号量主要用于阻塞线程,但不保证线程安全。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
生产者信号量初始化为8,每调用一次sem_wait(&psem),生产者信号量-1,每生产一个,消费者信号量加一,当生产者信号量为0时,阻塞。
消费者部分:每调用一次sem_wait(&csem),消费者信号量-1,每消费一个,生产者信号量加一,当消费者信号量为0时,阻塞。

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

// 创建一个互斥量
pthread_mutex_t mutex;
// 创建两个信号量
sem_t psem;
sem_t csem;

struct Node{
    int num;
    struct Node *next;
};

// 头结点
struct Node * head = NULL;

void * producer(void * arg) {

    // 不断的创建新的节点,添加到链表中
    while(1) {
        sem_wait(&psem);
        pthread_mutex_lock(&mutex);
        struct Node * newNode = (struct Node *)malloc(sizeof(struct Node));
        newNode->next = head;
        head = newNode;
        newNode->num = rand() % 1000;
        printf("add node, num : %d, tid : %ld\n", newNode->num, pthread_self());
        pthread_mutex_unlock(&mutex);
        sem_post(&csem);
    }

    return NULL;
}

void * customer(void * arg) {

    while(1) {
        sem_wait(&csem);
        pthread_mutex_lock(&mutex);
        // 保存头结点的指针
        struct Node * tmp = head;
        head = head->next;
        printf("del node, num : %d, tid : %ld\n", tmp->num, pthread_self());
        free(tmp);
        pthread_mutex_unlock(&mutex);
        sem_post(&psem);
       
    }
    return  NULL;
}

int main() {

    pthread_mutex_init(&mutex, NULL);
    sem_init(&psem, 0, 8);
    sem_init(&csem, 0, 0);

    // 创建5个生产者线程,和5个消费者线程
    pthread_t ptids[5], ctids[5];

    for(int i = 0; i < 5; i++) {
        pthread_create(&ptids[i], NULL, producer, NULL);
        pthread_create(&ctids[i], NULL, customer, NULL);
    }

    for(int i = 0; i < 5; i++) {
        pthread_detach(ptids[i]);
        pthread_detach(ctids[i]);
    }

    while(1) {
        sleep(10);
    }

    pthread_mutex_destroy(&mutex);

    pthread_exit(NULL);

    return 0;
}

C++实现生产者消费者模型

参考佬博客如下:
https://blog.csdn.net/qq_42214953/article/details/104878720
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用MATLAB计算波动率,股票历史价格为3.64 3.61 3.58 3.6 3.59 3.57 3.58 3.6 3.57 3.52 3.49 3.48 3.45 3.43 3.46 3.47 3.49 3.54 3.53 3.5 3.52 3.55 3.42 3.42 3.43 3.44 3.39 3.38 3.41 3.42 3.37 3.44 3.4 3.42 3.41 3.42 3.42 3.39 3.26 3.16 3.18 3.21 3.16 3.24 3.25 3.26 3.26 3.23 3.27 3.32 3.3 3.26 3.25 3.26 3.18 3.22 3.18 3.11 3.14 3.18 3.2 3.13 3.16 3.15 3.14 3.21 3.2 3.21 3.25 3.28 3.25 3.24 3.29 3.28 3.23 3.19 3.17 3.2 3.16 3.16 3.19 3.25 3.25 3.25 3.23 3.3 3.31 3.3 3.33 3.31 3.33 3.27 3.29 3.29 3.31 3.35 3.35 3.33 3.29 3.29 3.3 3.29 3.25 3.22 3.24 3.24 3.23 3.22 3.21 3.28 3.26 3.26 3.26 3.24 3.21 3.25 3.25 3.26 3.27 3.25 3.22 3.18 3.16 3.18 3.19 3.21 3.22 3.25 3.3 3.35 3.35 3.35 3.34 3.3 3.32 3.27 3.24 3.26 3.24 3.28 3.27 3.27 3.29 3.22 3.25 3.26 3.25 3.24 3.19 3.21 3.22 3.2 3.22 3.17 3.12 3.13 3.17 3.17 3.21 3.21 3.19 3.13 3.14 3.11 3.04 3.1 3.1 3.12 3.13 3.12 3.09 3.1 3.12 3.12 3.14 3.13 3.08 3.1 3.04 3.06 3.06 3.11 3.09 3.08 3.05 2.95 2.91 2.89 2.91 2.92 2.83 2.69 2.81 2.86 2.89 2.87 2.88 2.9 2.88 2.84 2.84 2.82 2.9 2.88 2.92 2.91 2.88 2.91 2.83 2.88 2.87 2.91 2.91 2.87 2.84 2.82 2.78 2.8 2.66 2.66 2.71 2.75 2.79 2.78 2.7 2.68 2.7 2.72 2.7 2.73 2.7 2.74 2.73 2.73 2.79 2.76 2.72 2.72 2.72 2.74 2.76 2.79 2.78 2.78 2.81 2.83 2.86 2.85 2.89 2.84 2.87 2.91 2.89 2.93 2.92 2.93 2.9 2.94 2.98 3.02 3.04 3.02 3.07 3.06 3.06 3.06 3.01 3 3.01 2.96 2.94 2.93 2.91 2.87 2.91 2.9 2.91 2.87 2.89 2.88 2.89 2.87 2.87 2.83 2.82 2.77 2.75 2.78 2.82 2.8 2.8 2.77 2.83 2.84 2.82 2.81 2.82 2.8 2.79 2.79 2.77 2.75 2.79 2.79 2.77 2.77 2.8 2.78 2.75 2.74 2.76 2.75 2.75 2.8 2.81 2.79 2.78 2.72 2.72 2.71 2.69 2.67 2.67 2.65 2.68 2.65 2.65 2.65 2.59 2.58 2.59 2.56 2.63 2.62 2.61 2.56 2.54 2.53 2.44 2.43 2.44 2.42 2.37 2.33 2.44 2.46 2.42 2.51 2.52 2.5 2.48 2.48 2.56 2.58 2.63 2.62 2.6 2.59 2.56 2.57 2.58 2.56 2.59 2.54 2.65 2.64 2.63 2.62 2.68 2.69 2.67 2.68 2.71 2.68 2.68 2.7 2.68 2.69 2.65 2.61 2.61 2.63 2.62 2.61 2.64 2.65 2.63 2.65 2.65 2.66 2.71 2.72 2.75 2.74 2.75 2.75 2.8 2.85 2.84 2.82 2.83 2.85 2.85 2.82 2.83 2.82 2.78 2.74 2.75 2.74 2.77 2.76 2.78 2.79 2.76 2.75 2.72 2.8 2.81 2.78 2.77 2.74 2.73 2.75 2.78 2.78 2.78 2.75 2.72 2.7 2.68 2.64 2.67 2.65 2.65 2.63 2.64 2.62 2.65 2.66 2.67 2.66 2.64 2.64 2.64 2.67 2.67 2.67 2.68 2.67 2.69 2.69 2.68 2.67 2.67 2.68 2.74 2.75 2.72 2.71 2.66 2.62 2.62 2.62 2.66 2.68 2.69 2.69 2.72 2.71 2.67 2.67 2.63 2.68 2.67 2.65 2.64 2.63 2.66 2.62 2.58 2.57 2.56 2.55 2.54 2.5 2.51
06-10
我们可以使用MATLAB中的函数计算历史价格的波动率,例如使用“std”函数计算价格的标准差,然后再将其乘以每年的交易天数的平方根来获得年化波动率。 以下是计算代码: ```matlab % 输入历史价格 price = [3.64 3.61 3.58 3.6 3.59 3.57 3.58 3.6 3.57 3.52 3.49 3.48 3.45 3.43 3.46 3.47 3.49 3.54 3.53 3.5 3.52 3.55 3.42 3.42 3.43 3.44 3.39 3.38 3.41 3.42 3.37 3.44 3.4 3.42 3.41 3.42 3.42 3.39 3.26 3.16 3.18 3.21 3.16 3.24 3.25 3.26 3.26 3.23 3.27 3.32 3.3 3.26 3.25 3.26 3.18 3.22 3.18 3.11 3.14 3.18 3.2 3.13 3.16 3.15 3.14 3.21 3.2 3.21 3.25 3.28 3.25 3.24 3.29 3.28 3.23 3.19 3.17 3.2 3.16 3.16 3.19 3.25 3.25 3.25 3.23 3.3 3.31 3.3 3.33 3.31 3.33 3.27 3.29 3.29 3.31 3.35 3.35 3.33 3.29 3.29 3.3 3.29 3.25 3.22 3.24 3.24 3.23 3.22 3.21 3.28 3.26 3.26 3.26 3.24 3.21 3.25 3.25 3.26 3.27 3.25 3.22 3.18 3.16 3.18 3.19 3.21 3.22 3.25 3.3 3.35 3.35 3.35 3.34 3.3 3.32 3.27 3.24 3.26 3.24 3.28 3.27 3.27 3.29 3.22 3.25 3.26 3.25 3.24 3.19 3.21 3.22 3.2 3.22 3.17 3.12 3.13 3.17 3.17 3.21 3.21 3.19 3.13 3.14 3.11 3.04 3.1 3.1 3.12 3.13 3.12 3.09 3.1 3.12 3.12 3.14 3.13 3.08 3.1 3.04 3.06 3.06 3.11 3.09 3.08 3.05 2.95 2.91 2.89 2.91 2.92 2.83 2.69 2.81 2.86 2.89 2.87 2.88 2.9 2.88 2.84 2.84 2.82 2.9 2.88 2.92 2.91 2.88 2.91 2.83 2.88 2.87 2.91 2.91 2.87 2.84 2.82 2.78 2.8 2.66 2.66 2.71 2.75 2.79 2.78 2.7 2.68 2.7 2.72 2.7 2.73 2.7 2.74 2.73 2.73 2.79 2.76 2.72 2.72 2.72 2.74 2.76 2.79 2.78 2.78 2.81 2.83 2.86 2.85 2.89 2.84 2.87 2.91 2.89 2.93 2.92 2.93 2.9 2.94 2.98 3.02 3.04 3.02 3.07 3.06 3.06 3.06 3.01 3 3.01 2.96 2.94 2.93 2.91 2.87 2.91 2.9 2.91 2.87 2.89 2.88 2.89 2.87 2.87 2.83 2.82 2.77 2.75 2.78 2.82 2.8 2.8 2.77 2.83 2.84 2.82 2.81 2.82 2.8 2.79 2.79 2.77 2.75 2.79 2.79 2.77 2.77 2.8 2.78 2.75 2.74 2.76 2.75 2.75 2.8 2.81 2.79 2.78 2.72 2.72 2.71 2.69 2.67 2.67 2.65 2.68 2.65 2.65 2.65 2.59 2.58 2.59 2.56 2.63 2.62 2.61 2.56 2.54 2.53 2.44 2.43 2.44 2.42 2.37 2.33 2.44 2.46 2.42 2.51 2.52 2.5 2.48 2.48 2.56 2.58 2.63 2.62 2.6 2.59 2.56 2.57 2.58 2.56 2.59 2.54 2.65 2.64 2.63 2.62 2.68 2.69 2.67 2.68 2.71 2.68 2.68 2.7 2.68 2.69 2.65 2.61 2.61 2.63 2.62 2.61 2.64 2.65 2.63 2.65 2.65 2.66 2.71 2.72 2.75 2.74 2.75 2.75 2.8 2.85 2.84 2.82 2.83 2.85 2.85 2.82 2.83 2.82 2.78 2.74 2.75 2.74 2.77 2.76 2.78 2.79 2.76 2.75 2.72 2.8 2.81 2.78 2.77 2.74 2.73 2.75 2.78 2.78 2.78 2.75 2.72 2.7 2.68 2.64 2.67 2.65 2.65 2.63 2.64 2.62 2.65 2.66 2.67 2.66 2.64 2.64 2.64 2.67 2.67 2.67 2.68 2.67 2.69 2.69 2.68 2.67 2.67 2.68 2.74 2.75 2.72 2.71 2.66 2.62 2.62 2.62 2.66 2.68 2.69 2.69 2.72 2.71 2.67 2.67 2.63 2.68 2.67 2.65 2.64 2.63 2.66 2.62 2.58 2.57 2.56 2.55 2.54 2.5

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值