2017-12-17 51 views
-2

私は教師とpthreadsライブラリが提供するMonitorクラスを使って、古典的なプロデューサ - コンシューマプログラムを作成しようとしています。私は単純なアルゴリズムの背後に論理があると思いますが、それを動作させるためには、バッファ内にいくつの要素があるかを把握する必要があります。私は理由を知らないが、その値の責任を負う変数はランダムな値を取っている。それをコンストラクタで0に初期化しても(他の2つの変数とともに)。誰もが私が作る間違いを指摘できますか? (教師のモニタライブラリは、それらの間のデータにアクセスするスレッドが1つだけであることを確認するenter()およびleave()関数を提供します。 。monitorメンバー変数はコンストラクタで初期化してもランダムな値を取得します - なぜですか?

main.cppに:

#include <stdio.h> 
#include <pthread.h> 
#include "mybuffer.h" 

void* client (void* parameters) 
{ 
    MyBuffer *p = (MyBuffer *) parameters; 
    char whatsinthebox; 
    int loop_counter = 0; 
    while(loop_counter < 5) { 
    printf("Client loop nr: %d\n",loop_counter); 
    whatsinthebox = p->Pop(); 
    printf("Product recieved: %c\n", whatsinthebox); 
    sleep(5); 
    loop_counter++; 

    } 
} 

void* producer (void* parameters) 
{ 
    MyBuffer *p = (MyBuffer *) parameters; 
    int loop_counter = 0; 
    char product = 'X'; 
    while(loop_counter<20) { 
    printf("Producer loop nr: %d\n",loop_counter); 
    p->Push(product); 
    printf("Product inserted: %c\n", product); 
    sleep(1); 
    loop_counter++; 
    } 
} 


int main() 
{ 
    MyBuffer *just_buffer = new MyBuffer(); 
    pthread_t thread1_id; 
    pthread_t thread2_id; 

    pthread_create (&thread1_id, NULL, &producer, &just_buffer); 
    pthread_create (&thread2_id, NULL, &client, &just_buffer); 

    pthread_join (thread1_id, NULL); 
    pthread_join (thread2_id, NULL); 

    delete just_buffer; 
    return 0; 
} 

mybuffer.h: - それはすでにポインターたとき

#ifndef MYBUFFER_H 
#define MYBUFFER_H 

#define MAX_ELEMENTS 9 
#define BUFFER_SIZE (MAX_ELEMENTS+1) 

#include "monitor.h" 

class MyBuffer: public Monitor 
{ 
private: 

    int data_in, data_out, elements_count; 
    char data[BUFFER_SIZE]; 
    Condition full, empty; 

public: 

    MyBuffer() 
    { 
    enter(); 
    data_in = data_out = 0; 
    elements_count = 0; 
    leave(); 
    } 

    int Push(char c) 
    { 
    enter(); 
    printf("Elements count before conditon in Push: %d\n",elements_count); 
    printf("data_in before conditon in Push: %d\n",data_in); 
    printf("data_out count before conditon in Push: %d\n",data_out); 
    if (elements_count == MAX_ELEMENTS) 
     wait(full); // queue full - can't push 
    data[data_in] = c; 
    data_in = (data_in + 1) % BUFFER_SIZE; 
    elements_count++; 
    if (elements_count == 1) 
     signal(empty); 
    leave(); 
    return 0; // no errors 
    } 

    char Pop() 
    { 
    char value; 
    enter();  
    if (elements_count == 0) 
     wait(empty); // queue empty - nothing to pop 
    value = data[data_out]; 
    data_out = (data_out + 1) % BUFFER_SIZE; 
    elements_count--; 
    if (elements_count == MAX_ELEMENTS - 1) 
     signal(full); 
    leave(); 
    return value; 
    } 
}; 

#endif 
+0

マイエラーがに4番目の引数を渡すことにした

だから私はこのような&せずにそれをPASSIのいずれかすべき参照としてのpthread_create - すでにポインタだったとき。 – Soonmoon

答えて

0

私のエラーは、参照としてpthread_createのために4番目の引数を渡すことにしました。

pthread_create (&thread1_id, NULL, &producer, just_buffer); 

それとも、このような通常の私のクラスのオブジェクトの代わりに、ポインタを定義します:

MyBuffer just_buffer; 
関連する問題