多线程编程总结——条件变量和互斥锁

本文详细介绍了在Linux环境下使用C语言实现线程间数据同步的方法,通过一个具体的生产者消费者模型示例,展示了如何利用互斥锁和条件变量来确保线程安全的数据操作。代码中包含了生产者和消费者的线程函数,以及用于数据存储的链表结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://blog.youkuaiyun.com/skyroben/article/details/72850109

 

进行了验证,在linux下验证OK。

 

 // cat product.cpp
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <error.h>
#include <assert.h>
#include <stdlib.h>
typedef  int DataType;
typedef struct listNode
{
        struct listNode* _next;
        DataType _data;
}node, *pnode, *plist;

pnode listHead = NULL;
//定义成全局,初始化简单
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t  need_product = PTHREAD_COND_INITIALIZER;

pnode CreateNode(DataType data)
{
        pnode newnode = (pnode)malloc(sizeof(node));
        newnode->_data = data;
        newnode->_next = NULL;
        return newnode;
}

void Init(plist *list)
{
        if (NULL != list)
        {
                *list = NULL;
        }
}

void PushFront(plist *list, DataType d)
{
        assert(list != NULL);
        pnode newnode = CreateNode(d);
        pnode cur = *list;
        if (cur == NULL)
        {
                *list = newnode;
                newnode->_next = NULL;
        }
        else
        {
                *list = newnode;
                newnode->_next = cur;
        }
        return;
}

void PopFront(plist *list)
{
        assert(list != NULL);
        pnode cur = *list;
        if (cur == NULL)
        {
                return;
        }
        else if (cur->_next == NULL)
        {
                cur = cur->_next;
                free(cur);
                *list = NULL;
        }
        else
        {
                *list = cur->_next;
                free(cur);
                cur = NULL;
        }
        return;
}

void Destroy(plist  *list)
{
        pnode cur = *list;
        while (cur != NULL)
        {
                *list = cur->_next;
                cur = *list;
        }
        return;
}
void ShowList(plist list)
{
        pnode cur = list;
        while (cur != NULL)
        {
                printf("%d ", cur->_data);
                cur = cur->_next;
        }
        printf("\n");
}


void *product(void * _val)
{
        while (1)
        {
                sleep(1);
                //加锁
                pthread_mutex_lock(&lock);
                int num = rand() % 100;
                Init(&listHead);
                PushFront(&listHead, num);
                printf("call consum:product success and the value is %d\n", num);
                //解锁
                pthread_mutex_unlock(&lock);
                //唤醒等待目标变量的线程
                pthread_cond_signal(&need_product);
        }
}


void *consum(void *_val)
{
        while (1)
        {
                pthread_mutex_lock(&lock);
                while (listHead == NULL)
                {
                        //等待目标条件变量,锁使得pthread_cond_wait操作的原子性
                        pthread_cond_wait(&need_product, &lock);
                }
                printf("call product:consum success and the value is %d\n", listHead->_data);
                PopFront(&listHead);
                ShowList(listHead);
                pthread_mutex_unlock(&lock);
        }
        return NULL;
}

int main()
{
        pthread_t t_product;
        pthread_t t_consum;
        pthread_create(&t_product, NULL, product, NULL);
        pthread_create(&t_consum, NULL, consum, NULL);
        //回收线程
        pthread_join(t_product, NULL);
        pthread_join(t_consum, NULL);
        return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值