[ create a new paste ] login | about

Link: http://codepad.org/sz7sgTGp    [ raw code | output | fork ]

C++, pasted on Feb 23:
#include <pthread.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdint.h>

int napln;
int valov;
int pocetKobyl;
int zaludok = 10;
//t nespi = 0;
int counter= 1;

pthread_mutex_t mutex;
pthread_cond_t data_cond = PTHREAD_COND_INITIALIZER;


void* rancher(void* data)
{
    while(1){
    pthread_mutex_lock(&mutex);

    while(valov > 0)
    {

        pthread_cond_wait(&data_cond, &mutex);

    }


    printf("valov naplneny.\n");
    valov = napln;

    pthread_mutex_unlock(&mutex);
    }
}


void kobyla()
{
    int cislokobyly = 0;
    while(1)
    {
    pthread_mutex_lock(&mutex);
    if(cislokobyly == 0){cislokobyly = counter++;}
    pthread_mutex_unlock(&mutex);


    sleep(1+(rand()%10));


        pthread_mutex_lock(&mutex);

        if(valov == 0)
        {
            printf("MMMMYYYYYHHHHAAAAA \n");
            printf("Prazdny valov.\n");
            pthread_cond_signal(&data_cond);

        }else
        {
            if(valov >= zaludok){
                valov -= zaludok;
                printf("kobyla %d papa. Ostalo vo valove %d\n ",cislokobyly,valov);
            }else
            {
                printf("kobyla %d spapala len %d\n",cislokobyly,valov);
                valov = 0;
                pthread_cond_signal(&data_cond);
            }


        }

        pthread_mutex_unlock(&mutex);

    }


}





void* thread_main(void* data)
{
    kobyla();
}

int main(int argc, const char *argv[])
{
    if(argc !=3)
    {
        printf("chyba v argumentoch.");
    }else{

     valov = atoi(argv[2]);
     napln = valov;
     pocetKobyl = atoi(argv[1]);

    pthread_t pKobyl[pocetKobyl],parancher;
        pthread_mutex_init(&mutex,NULL);

    int i=0;
    for(i=0;i<pocetKobyl;++i)
    {
        pthread_create(&pKobyl[i], NULL, &thread_main, NULL);
    }

        pthread_create(&parancher, NULL, &rancher, NULL);
    //cakam na dokoncenie vsetkych vlakien
    for(i=0;i<pocetKobyl;++i)
    {
        pthread_join(pKobyl[i],NULL);
    }
        pthread_join(parancher,NULL);




    pthread_mutex_destroy(&mutex);

return 0;
}
}


Output:
1
2
3
In function 'int main(int, const char**)':
Line 103: error: ISO C++ forbids variable-size array 'pKobyl'
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: