From: ishikawa@agusa.nuee.nagoya-u.ac.jp (Akira Ishikawa)
Newsgroups: fj.lang.c,fj.sys.luna,fj.sys.next
Subject: g++onMACH
Date: 11 Oct 92 15:55:44 GMT
Distribution: fj
Organization: Dept. of Electric Engineering, Nagoya Univ.

$B@P@n!wL>8E20Bg3X$H?=$7$^$9!#(J

$B<ALd$,$"$j$^$9!#(J
$B;d$O(JMACH$B>e$G(Jc++$B$r$D$+$C$F%W%m%0%i%`$r=q$$$F$$$^$9!#(J
$B2<$N$h$&$J%F%9%H%W%m%0%i%`$r;n$7$F$_$?$H$3$m!"(JNeXT$B$N(Jcc++$B$H(JLUNA2$B$N(Jg++-1.39
$B$G$O@5>o$KF0$/$N$G$9$,!"(Jg++-2.2.2$B$@$H(JBus error$B$K$J$C$FF0$-$^$;$s!#(J
$B2?$,860x$J$N$G$7$g$&$+(J?
($B$3$N%W%m%0%i%`$O(JUNIX MAGAZINE 1990.4 $B$N%5%s%W%k%W%m%0%i%`(J
$B$r(Jc++$BMQ$K=q$-D>$7$?J*$G$9!#(J)
$B$I$&$>$h$m$7$/$*4j$$$7$^$9!#(J

-----------$B$3$3$+$i(J-------------

#define ON      1
#define OFF     0

#define BUFFER_SIZE     10
#define INPUT_BUF_SIZE  5

extern "C"{
#include <stdio.h>
#include "cthreads.h"
}

class Buffer {
 private:
    struct mutex        lock;
    char                *chars;
    int                 size;
    int                 px;
    int                 cx;
    int                 count;
    struct condition    non_empty;
    struct condition    non_full;
 public:
    Buffer(int);
    void buffer_put(char);
    char buffer_get(void);
};


Buffer::Buffer(int argsize)
{
    mutex_init(&lock);
    size = argsize;
    chars = new char [argsize];
    px = cx = count = 0;
    condition_init(&non_empty);
    condition_init(&non_full);
}

void
Buffer::buffer_put(char ch)
{
    mutex_lock(&lock);
    while (count == size) {
        condition_wait(&non_full, &lock);
    }
    chars[px] = ch;
    px = (px + 1) % size;
    count += 1;
    condition_signal(&non_empty);
    mutex_unlock(&lock);
}

char
Buffer::buffer_get(void)
{
    char ch;

    mutex_lock(&lock);
    while (count == 0) {
        condition_wait(&non_empty, &lock);
    }
    ch = chars[cx];
    cx = (cx + 1) % size;
    count -= 1;
    condition_signal(&non_full);
    mutex_unlock(&lock);
    return (ch);
}


void producer(Buffer *b)
{
    char input_buf[INPUT_BUF_SIZE];
    int  ch, i=0,j=0;
    while(1){
        for(i=0;i<INPUT_BUF_SIZE-1;i++)
            input_buf[i]=getchar();
        input_buf[i]='\0';
        fprintf(stderr, "producer: %s\n", input_buf);
        for (i = 0; input_buf[i]; i++) {
            b->buffer_put(input_buf[i]);
            if(input_buf[i]==EOF){
                j=1;
                break;
            }
        }
            if(j==1)
                break;
    }
}

void consumer(Buffer *b)
{
    int ch;
    int flag_first_column = ON;
    
    while ((ch = b->buffer_get()) != EOF) {
        if (flag_first_column == ON) {
            fprintf(stderr, "consumer: ");
            flag_first_column = OFF;
        }
        putc(ch, stdout);
        if (ch == '\n') {
            flag_first_column = ON;
        }
    }
}

main(int argc, char **argv)
{
    Buffer *b;
    cthread_init();
    b = new Buffer(BUFFER_SIZE);
    cthread_detach(cthread_fork((cthread_fn_t &)producer, b));
    consumer(b);
}


---------------$B$3$3$^$G(J---------------
---
*****************************************************************
        $B@P@nN<!wL>8E20Bg3X9)3XItEE5$7O3X2J(J $B0$Ap8&5f<<(J $B#M#1(J
   ishikawa@agusa.nuee.nagoya-u.ac.jp  052-781-5111 ($BFb@~(J 4434)
*****************************************************************
ize;
    count -= 1;
    condition_signal(&non_full);
    mutex_unlock(&lock);
    return (ch);
}


void producer(Buffer *b)
{
    char input_buf[$BCf(J$BZD(J
Newsgroups: fj.lang.postscript