linux平台C++多线程代码示例

4 篇文章 0 订阅
4 篇文章 0 订阅

读写锁示例

#include <unistd.h>
#include<iostream>

int number =0;
//define read and write lock
pthread_rwlock_t lock;
//读写回调函数
void * write_func(void * arg)
{
    while(1)
    {
        pthread_rwlock_wrlock(&lock);
        number++;
        printf("== write:%lu,%d\n",pthread_self(),number);
        pthread_rwlock_unlock(&lock);
        usleep(5000);
    }
}
void * read_func(void * arg)
{
    while(1)
    {
        pthread_rwlock_rdlock(&lock);
        printf("== read%lu,%d\n",pthread_self(),number);
        pthread_rwlock_unlock(&lock);
        usleep(5000);
    }
}



int main()
{
    //创建子线程,和锁
    pthread_rwlock_init(&lock,NULL);
    pthread_t pt[8];
    for (int i=0;i<3;i++)
    {
        pthread_create(&pt[i],NULL,write_func,NULL);
    }
    for (int i=3;i<8;i++)
    {
        pthread_create (&pt[i],NULL,read_func,NULL);
    }

    //等待子线程退出
    for (int i=0;i<8;i++)
    {
        pthread_join(pt[i],NULL);

    }
    //销毁锁
    pthread_rwlock_destroy(&lock);
    return 0;
}

条件变量实现生产者消费者示例

为了便于观察,作为共享数据的链表让其始终保持不多于一个节点。

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

//初始化互斥锁和条件变量
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond =PTHREAD_COND_INITIALIZER;

typedef struct listNode Node;
struct listNode
{
    int num;
    struct listNode * pNext;
};


Node *phead=NULL;//define the head point;

void * producter(void * arg)
{
    for(;;)
    {
        //只产生一个节点,即停止生产
        while(phead==NULL)
        {
            //产生一个新的节点
            Node *tmpNode=(Node*)malloc(sizeof(Node));
            tmpNode->num=rand()%1000+1;
            pthread_mutex_lock(&mutex);
            //把节点加入到链表
            tmpNode->pNext=phead;//头插法,即新节点的指针域指向链表的头指针
            phead=tmpNode;//新节点的指针变为链表的头指针。
            cout<<"producing the num = "<<tmpNode->num<<endl;
            pthread_mutex_unlock(&mutex);
            //添加结束,发送信号
            pthread_cond_signal(&cond);
        }
        //  sleep(rand()%1);//方便观察
    }
    return NULL;
}

void * consumer(void * arg)
{
    for (;;)
    {
        pthread_mutex_lock(&mutex);
        //判断链表是否为空,如果为空,则调用条件变量函数,阻塞线程
        while (phead==NULL)
        {
            //阻塞线程,释放锁,收到signal后,释放线程,获得锁
            pthread_cond_wait(&cond,&mutex);
        }
        //如果不为空,则用头删法删除链表的头节点
        //定义一个临时节点保存被删除的头部节点
        Node *tmpNode=phead;
        phead=phead->pNext;//头节点的指针域存的地址作为新的头节点
        cout<<"---------------consumer--------"<<tmpNode->num<<endl;
        free(tmpNode);
        pthread_mutex_unlock(&mutex);
    }
    return NULL;
}



int main(void)
{
    //定义并创建生产者和消费者线程
    pthread_t ptid,ctid;
    pthread_create(&ptid,NULL,producter,NULL);
    pthread_create(&ctid,NULL,consumer,NULL);

    //等待线程结束
    pthread_join(ptid,NULL);
    pthread_join(ctid,NULL);

    return 0;

}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值