C++编程实现生产者/消费者模型(linux系统编程)


参考了这篇实现相同功能的java程序http://www.cnblogs.com/linjiqin/archive/2011/04/15/2016820.html

代码如下:

p_c.h

/*
 * p_c.h
 *
 *  Created on: 2016年6月28日
 *      Author: ZhangXianYi
 */

#ifndef P_C_H_
#define P_C_H_
//============================================================================
// Name        : p_c.cpp
// Author      : ZhangXianYi
// Version     : 1.0
// Copyright   : Your copyright notice
// Description : 模拟生产者/消费者的程序
//============================================================================
//析构函数  和  赋初值   注意一下
#include <iostream>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>

class SynchronizedStack
{
public:
    char ss[100];
    int index;
    pthread_mutex_t mutex;

    SynchronizedStack ();

    ~SynchronizedStack ();
    void push(char c);
    char pop();

};

void * Consumer(void *arg);

void * Producer(void *arg);


#endif /* P_C_H_ */


p_c.cpp

//============================================================================
// Name        : p_c.cpp
// Author      : ZhangXianYi
// Version     : 1.0
// Copyright   : Your copyright notice
// Description : 模拟生产者/消费者的程序
//============================================================================
//析构函数  和  赋初值   注意一下

#include "p_c.h"

using namespace std;

SynchronizedStack::SynchronizedStack ()
{
    index = -1;
    for(int i=0;i<100;i++)
        ss[i] = 'a';
    pthread_mutex_init(&mutex,NULL);
}

SynchronizedStack::~SynchronizedStack ()
{
    pthread_mutex_destroy(&mutex);
}

void SynchronizedStack::push(char c)
{

    if(index<100)
        ss[++index] = c;
    else
        cout << "ss满了!"<< endl;

}
char SynchronizedStack::pop()
{
    char s=')';

    if(index>=0)
        s = ss[index--];
    else
        cout << "栈空了!" << endl;

    return s;
}

void * Consumer(void *arg)
{
    SynchronizedStack  *temp = (SynchronizedStack  *)arg;
    char s;
    for(int i=0;i<30;i++)
    {
        pthread_mutex_lock(&(temp->mutex));
        s = temp->pop();
        if(s!=')')
            cout << "Consumed:" << s << endl;
        pthread_mutex_unlock(&(temp->mutex));
        sleep(1);
    }
}

void * Producer(void *arg)
{
    SynchronizedStack  *temp = (SynchronizedStack  *)arg;

    for(int i=0;i<26;i++)
    {
        pthread_mutex_lock(&(temp->mutex));
        temp->push((char)(i+65));
        cout << "Produced:" << temp->ss[temp->index] << endl;
        pthread_mutex_unlock(&(temp->mutex));
        sleep(1);
    }
}


main.cpp

/*
 * Main.cpp
 *
 *  Created on: 2016年6月28日
 *      Author: administrator
 */

#include "p_c.h"

int main()
{
    pthread_t pid1,pid2;
    int error1,error2;
    error1 = 0;
    error2 = 0;
    SynchronizedStack * quyu = new SynchronizedStack ();
    error2=pthread_create(&pid2,NULL,Producer,quyu);
    error1=pthread_create(&pid1,NULL,Consumer,quyu);
    if(error1!=0 || error2!=0)
    {
        printf("pthread_cread failed");
        return 0;
    }
    pthread_join(pid1,NULL);
    pthread_join(pid2,NULL);
    return 0;
}






最初也尝试过将加解锁的两行放在pop push函数里,结果打印的时候回串,所以放到线程要执行的函数里去了。

某一次运行结果如下:

      0 [main] p_c 16612 find_fast_cwd: WARNING: Couldn't compute FAST_CWD pointer.  Please report this problem to
the public mailing list cygwin@cygwin.com
Produced:A
Consumed:A
Produced:B
Consumed:B
Produced:C
Consumed:C
Produced:D
Consumed:D
Produced:E
Consumed:E
Produced:F
Consumed:F
Produced:G
Consumed:G
Produced:H
Consumed:H
栈空了!
Produced:I
Consumed:I
Produced:J
Consumed:J
Produced:K
Consumed:K
Produced:L
Consumed:L
Produced:M
Consumed:M
Produced:N
Consumed:N
Produced:O
Consumed:O
Produced:P
Produced:Q
Consumed:Q
Consumed:P
Produced:R
Consumed:R
Produced:S
Consumed:S
Produced:T
Produced:U
Consumed:U
Consumed:T
Produced:V
Consumed:V
Produced:W
Produced:X
Consumed:X
Consumed:W
Produced:Y
Produced:Z
Consumed:Z
Consumed:Y
栈空了!
栈空了!
栈空了!



参考了这篇实现相同功能的java程序http://www.cnblogs.com/linjiqin/archive/2011/04/15/2016820.html

代码如下:

p_c.h

/*
 * p_c.h
 *
 *  Created on: 2016年6月28日
 *      Author: ZhangXianYi
 */

#ifndef P_C_H_
#define P_C_H_
//============================================================================
// Name        : p_c.cpp
// Author      : ZhangXianYi
// Version     : 1.0
// Copyright   : Your copyright notice
// Description : 模拟生产者/消费者的程序
//============================================================================
//析构函数  和  赋初值   注意一下
#include <iostream>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>

class SynchronizedStack
{
public:
    char ss[100];
    int index;
    pthread_mutex_t mutex;

    SynchronizedStack ();

    ~SynchronizedStack ();
    void push(char c);
    char pop();

};

void * Consumer(void *arg);

void * Producer(void *arg);


#endif /* P_C_H_ */


p_c.cpp

//============================================================================
// Name        : p_c.cpp
// Author      : ZhangXianYi
// Version     : 1.0
// Copyright   : Your copyright notice
// Description : 模拟生产者/消费者的程序
//============================================================================
//析构函数  和  赋初值   注意一下

#include "p_c.h"

using namespace std;

SynchronizedStack::SynchronizedStack ()
{
    index = -1;
    for(int i=0;i<100;i++)
        ss[i] = 'a';
    pthread_mutex_init(&mutex,NULL);
}

SynchronizedStack::~SynchronizedStack ()
{
    pthread_mutex_destroy(&mutex);
}

void SynchronizedStack::push(char c)
{

    if(index<100)
        ss[++index] = c;
    else
        cout << "ss满了!"<< endl;

}
char SynchronizedStack::pop()
{
    char s=')';

    if(index>=0)
        s = ss[index--];
    else
        cout << "栈空了!" << endl;

    return s;
}

void * Consumer(void *arg)
{
    SynchronizedStack  *temp = (SynchronizedStack  *)arg;
    char s;
    for(int i=0;i<30;i++)
    {
        pthread_mutex_lock(&(temp->mutex));
        s = temp->pop();
        if(s!=')')
            cout << "Consumed:" << s << endl;
        pthread_mutex_unlock(&(temp->mutex));
        sleep(1);
    }
}

void * Producer(void *arg)
{
    SynchronizedStack  *temp = (SynchronizedStack  *)arg;

    for(int i=0;i<26;i++)
    {
        pthread_mutex_lock(&(temp->mutex));
        temp->push((char)(i+65));
        cout << "Produced:" << temp->ss[temp->index] << endl;
        pthread_mutex_unlock(&(temp->mutex));
        sleep(1);
    }
}


main.cpp

/*
 * Main.cpp
 *
 *  Created on: 2016年6月28日
 *      Author: administrator
 */

#include "p_c.h"

int main()
{
    pthread_t pid1,pid2;
    int error1,error2;
    error1 = 0;
    error2 = 0;
    SynchronizedStack * quyu = new SynchronizedStack ();
    error2=pthread_create(&pid2,NULL,Producer,quyu);
    error1=pthread_create(&pid1,NULL,Consumer,quyu);
    if(error1!=0 || error2!=0)
    {
        printf("pthread_cread failed");
        return 0;
    }
    pthread_join(pid1,NULL);
    pthread_join(pid2,NULL);
    return 0;
}






最初也尝试过将加解锁的两行放在pop push函数里,结果打印的时候回串,所以放到线程要执行的函数里去了。

某一次运行结果如下:

      0 [main] p_c 16612 find_fast_cwd: WARNING: Couldn't compute FAST_CWD pointer.  Please report this problem to
the public mailing list cygwin@cygwin.com
Produced:A
Consumed:A
Produced:B
Consumed:B
Produced:C
Consumed:C
Produced:D
Consumed:D
Produced:E
Consumed:E
Produced:F
Consumed:F
Produced:G
Consumed:G
Produced:H
Consumed:H
栈空了!
Produced:I
Consumed:I
Produced:J
Consumed:J
Produced:K
Consumed:K
Produced:L
Consumed:L
Produced:M
Consumed:M
Produced:N
Consumed:N
Produced:O
Consumed:O
Produced:P
Produced:Q
Consumed:Q
Consumed:P
Produced:R
Consumed:R
Produced:S
Consumed:S
Produced:T
Produced:U
Consumed:U
Consumed:T
Produced:V
Consumed:V
Produced:W
Produced:X
Consumed:X
Consumed:W
Produced:Y
Produced:Z
Consumed:Z
Consumed:Y
栈空了!
栈空了!
栈空了!



阅读更多
文章标签: c++
个人分类: c++基础
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

C++编程实现生产者/消费者模型(linux系统编程)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭