SDL实现生产者消费者

环境:

阿里云主机

CPU 1     内存: 1GB    数据盘:  20G

操作系统: Ubuntu 12.04 64

 

1.       SDL安装

[root@iZ25ml7xjj1Z:/usr/lixian]$sudo aptitude install libsdl1.2debian

 

2.       生产者消费者源代码

/*************************************************

Name:        producerConsumer.cpp

Author:      Xian Li

Created:      2014-11-2

Compiler:     g++

Environment:  Aliyun host Ubuntu 12.04 64

*************************************************/

 

 

#include <stdio.h> 

#include <SDL/SDL.h> 

#include <SDL/SDL_thread.h> 

#include <unistd.h>

 

#define BUFFER_LEN 4   //缓冲区队列长度

 

SDL_mutex *mutex;       //互斥锁变量

SDL_cond *cond;             //条件变量

 

int buffer[BUFFER_LEN];  //缓冲区

 

int head = 0,tail = 0;     //队列头指针和尾指针

 

int productID = 1;      //产品ID

int productCount = 0;   //队列中产品计数

 

static int producerThread(void *ptr){ 

    while(1){ 

        if (SDL_LockMutex(mutex) == 0) {    //获取锁,进入临界区

         while(BUFFER_LEN==productCount) //如果缓冲区已满,释放锁,并等待缓冲区出现空闲空间

             {

                   SDL_CondWait(cond, mutex);

                printf("Buffer is full, producer is waiting......");

             }

 

             buffer[head] = productID;

            head = (head+1)%BUFFER_LEN;

            printf("Producer has produced a product %d\n", productID); 

             

                 productID++;

                 productCount++;

            SDL_CondSignal(cond); 

            SDL_UnlockMutex(mutex); 

            sleep(rand()%5); 

 

        } else { 

            fprintf(stderr, "Couldn't lock mutex\n"); 

        } 

         

    } 

  

    return 0; 

 

static int consumerThread(void *ptr){ 

    while(1){ 

        if (SDL_LockMutex(mutex) == 0)

         { 

            while(0==productCount){  //如果缓冲区为空,释放锁,并等待缓冲区非空

                  SDL_CondWait(cond, mutex); 

                             printf("\t\t Buffer is empty, consumer is waiting......\n");

            } 

            printf("\t\t Consumer consumed product %d\n", buffer[tail]); 

            tail = (tail+1)%BUFFER_LEN;

                 productCount--;

                 SDL_CondSignal(cond); 

            SDL_UnlockMutex(mutex); 

            sleep(rand()%5); 

 

        }

         else

         { 

            fprintf(stderr, "Couldn't lock mutex\n"); 

        } 

         

    } 

    return 0; 

 

int main(int argc, char *argv[]) 

 

    mutex = SDL_CreateMutex();    //申请锁变量

    if (!mutex) { 

        fprintf(stderr, "Couldn't create mutex\n"); 

        return -1; 

    } 

 

    cond = SDL_CreateCond();     //申请条件变量

    if (!cond) { 

        fprintf(stderr, "Couldn't create cond\n"); 

        return -1; 

    } 

 

    SDL_Thread *threadA; 

    SDL_Thread *threadB; 

    int         threadReturnValueA; 

    int         threadReturnValueB; 

 

 

    threadA = SDL_CreateThread(producerThread, (void *)NULL); 

    if (NULL == threadA) { 

        printf("\nSDL_CreateThread failed: %s\n", SDL_GetError()); 

    } 

     

    threadB = SDL_CreateThread(consumerThread, (void *)NULL); 

    if (NULL == threadB) { 

        printf("\nSDL_CreateThread failed: %s\n", SDL_GetError()); 

    } 

      

    SDL_WaitThread(threadA, &threadReturnValueA); 

    SDL_WaitThread(threadB, &threadReturnValueB); 

 

    return 0; 

 

3.       编译、连接,生成可执行文件。

[root@iZ25ml7xjj1Z:/usr/lixian]$g++ -o exc producerConsumer.cpp –lSDL

 

4.         运行,结果如下:
SDL_producer_consumer.png
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值