zoukankan      html  css  js  c++  java
  • 第一次编译linux下的非helloword代码==

      其实就是书上简单的多线程生产者-消费者模型

    include <stdio.h>
    #include <pthread.h>
    #define MAX 1000
    pthread_mutex_t the_mutex;
    pthread_cond_t condc,condp;
    int buffer=0;
    
    void* producer(void* ptr)
    {
            int i;
            for(i=1;i<=MAX;i++)
            {
                    pthread_mutex_lock(&the_mutex);
                    while(buffer!=0) pthread_cond_wait(&condp,&the_mutex);
                    buffer=i;
                    printf("%d product has been product.
    ",buffer);
                    pthread_cond_signal(&condc);
                    pthread_mutex_unlock(&the_mutex);
            }
            pthread_exit(0);
    }
    
    void* consummer(void* ptr)
    {
            int i;
            for(i=1;i<=MAX;i++)
            {
                pthread_mutex_lock(&the_mutex);
                    while(buffer==0) pthread_cond_wait(&condc,&the_mutex);
                    buffer=0;
                    printf("product has been consume.
    ");
                    pthread_cond_signal(&condp);
                    pthread_mutex_unlock(&the_mutex);
            }
            pthread_exit(0);
    }
    
    int main(int argc,char** argv)
    {
            pthread_t pro,con;
            pthread_cond_init(&condc,0);
            pthread_cond_init(&condp,0);
            pthread_create(&con,0,consummer,0);
            pthread_create(&pro,0,producer,0);
            pthread_join(pro,0);
            pthread_join(con,0);
            pthread_cond_destroy(&condc);
            pthread_cond_destroy(&condp);
            pthread_mutex_destroy(&the_mutex);
    }

    结果如下:

  • 相关阅读:
    STM32F2系列系统时钟默认配置
    在电源上叠加一个脉冲信号,模拟一个干扰信号
    const用法
    指向指针的指针
    Judge Route Circle
    汉明距离
    绘制三角形
    OpenGL工作流程
    OpenGL环境搭建
    next()方法 执行下一个中间件 类似than
  • 原文地址:https://www.cnblogs.com/manch1n/p/10317440.html
Copyright © 2011-2022 走看看